Chai's Assert Not Working Properly

I'm using Chai's assert for checking the values of responses returned from a Server that I've made. However, assert is returning true even when the values passed to it are not equal. Let me give an example:

describe('Tests', function() {
    it('Simple Query', function() {

        // assert.equal(2,3); //returns false

        controller1.simpleQuery(true, 4, "hello", null, function(error, response, context) {

            assert.equal(2,3); //returns true

        });

    });
});

Ok so here's the issue: both the assert statements in this code should return false because 2 is not equal to 3. However, right now the assert statement before the simpleQuery() function call is returning false (which is the correct behavior). And the assert statment within the simpleQuery() call is returning true.

This is honestly really weird and I can't seem to figure out why it's doing this. Can anybody help me figure this out?

1 Answer

The problem is that your simpleQuery is async. As a result test will complete before callback was even called. You need to show mocha your test is async by using done callback.

describe('Tests', function() {
    it('Simple Query', function(done) {

        // assert.equal(2,3); //returns false

        controller1.simpleQuery(true, 4, "hello", null, function(error, response, context) {

            assert.equal(2,3); //returns true
            done();

        });

    });
});
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest