Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha js assertion hangs while using promise?

"use strict";
let assert = require("assert");

describe("Promise test", function() {
  it('should pass', function(done) {
    var a = {};
    var b = {};
    a.key = 124;
    b.key = 567;
    let p = new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve();
        }, 100)
    });

    p.then(function success() {

        console.log("success---->", a, b);
        assert.deepEqual(a, b, "response doesnot match");
        done();
    }, function error() {

        console.log("error---->", a, b);
        assert.deepEqual(a, b, "response doesnot match");
        done();
    });
 });
});

Output: output result

I am using node v5.6.0. The test seems to hang for assert when values don't match.

I tried to check if it's issue with assert.deepEqual using setTimeout but it works fine.

But it fails while using Promise and hangs if the values don't match.

like image 785
actor203 Avatar asked Apr 14 '16 09:04

actor203


People also ask

How do you test promise-based code in Mocha?

Since you may be in the midst of wrestling your failing tests back into the green, let’s jump straight into how you can effectively test promise-based code in Mocha. If a function returns a promise that you want to assert on the result of the function, all you need to do for it to work effectively with Mocha is return the promise in your it block.

How do I handle async promise in Mocha?

The first option for using Mocha to handle async methods is with the done callback. And while there are other, more elegant ways to handle async promise code in Mocha, the done callback still has some key advantages.

What is the assert module in Mocha?

The strict property of the assert module will allow us to use special equality tests that are recommended by Node.js and are good for future-proofing, since they account for more use cases. Before we go into writing tests, let’s discuss how Mocha organizes our code. Tests structured in Mocha usually follow this template:

Why do we do automated testing with Mocha and assert?

This output highlights the benefit of why we do automated testing with Mocha and assert. Because our tests are scripted, every time we run npm test, we verify that all our tests are passing. We did not need to manually check if the other code is still working; we know that it is because the test we have still passed.


2 Answers

You're getting that error, because your test is never finished. This assertion: assert.deepEqual(a, b, "response doesnot match"); throws an error, and so as you haven't catch block, done callback is never called.

You should add catch block in the end of promise chain:

...
p.then(function success() {
    console.log("success---->", a, b);
    assert.deepEqual(a, b, "response doesnot match");
    done();
}, function error() {
    console.log("error---->", a, b);
    assert.deepEqual(a, b, "response doesnot match");
    done();
})
.catch(done); // <= it will be called if some of the asserts are failed
like image 158
alexmac Avatar answered Nov 04 '22 10:11

alexmac


Since you are using a Promise, I suggest to simply return it at the end of the it. Once the Promise is settled (fulfilled or rejected), Mocha will consider the test done and it will consume the Promise. In case of a rejected Promise, it will use its value as the thrown Error.

NB: do not declare a done argument if you are returning a Promise.

like image 41
David da Silva Avatar answered Nov 04 '22 11:11

David da Silva