Search code examples
javascriptmocha.jsassertchai

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?


Solution

  • 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();
    
            });
    
        });
    });