Trần Dương Trần Dương - 1 month ago 21
Javascript Question

Mocha Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test

I tried to test a async function. But mocha always log timeout error.

// iframe.js
var iframe_client_util = {};
iframe_client_util.findByKeyFrame = function (keyFrame, cb) {
ModelIframeClient.find({key_frame: keyFrame}).exec(function (err, rs) {
if (err) return cb(err, null);

cb(null, rs);
});
};



// test.js
var assert = require("assert");
var iframe_util = require("./iframe");
describe('1. _checkAuthorization', function () {
it("Using setTimeout to simulate asynchronous code!", function (done) {
iframe_util.findByKeyFrame("598ad74b1ca8870b0072b6e9", function (err, result) {
assert.equal(true, true);
done();
});
});
});


Then mocha log this error when I run
mocha test.js
:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.


I follow this document: https://mochajs.org/#asynchronous-code.

Answer Source

I resolved this problem . I didn't connect to mongoDB, iframe_util.findByKeyFrame function cannot run . It need mongoose.connect(); before describes.