"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:
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.
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.
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.
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:
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With