Does Mocha js assertion hang when using promises?

"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 freeze for approval when the values ​​do not match.

I tried to check if there is a problem with assert.deepEqual using setTimeout, but it works fine.

But it does not work when using Promise and freezes if the values ​​do not match.

+5
source share
2 answers

You get this error because your test never ends. This statement is: assert.deepEqual(a, b, "response doesnot match"); throws an error, and since you are not blocking the block, the done callback is never called.

You should add a catch at the end of the 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 
+4
source

Since you are using Promise, I suggest just returning it at the end of it . After the Promise is settled (fulfilled or rejected), Urine will review the completed test and will absorb the promise. In the case of a rejected promise, it will use its value as a discarded error.

NB: do not declare done if you return Promise.

+2
source

Source: https://habr.com/ru/post/1247111/


All Articles