I had the error Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test., but in this test, done() not used. in before() function, promise returned.
I tried to look into this issue: 1, any done() hidden? 2, I searched in the repo, but I did not find Ensure the done() callback is being called in this test. in the source codes.
By the way, my mocha version is 2.1.0
Is it because of bluebird is used and the returned promise not resolved?
Can you show your code?
I believe in newer versions of Mocha that error message mentions ensuring that the returned promise either resolves or rejects.
I have the same problem
describe('some test', _ => {
before(done => {
begin(_ => {
console.log('Done should be called')
done()
})
})
it('a test', _ => {
console.log('I\'m called, i\'m happy :)')
})
})
output:
Done should be called
I'm called, i'm happy :)
1) a test
0 passing (2s)
1 failing
1) some test a test:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
"mocha": "^3.5.3"
My codes:
describe('DELETE', () => {
before(function() {
this.callback = (instance) => {
this.instance= instance;
};
return this.MyOBJ.OBJ.create(null, this.data)
.then((obj) => {
this.obj = obj;
this.MyOBJ.on('UPDATE', this.callback);
})
.delay(500)
.then(() => this.obj.destroy())
.delay(250);
});
after(function() {
this.MyOBJ.removeListener('UPDATE', this.callback);
});
it('xxx', function() {
});
});
Hi @xahon,
Change the _ to () for the test and it should work.
Mocha determines whether a test is synchronous or asynchronous based on the argument to the test function, but it doesn't parse the source or anything like that to determine its name, so _ as the parameter to your test function is presumed by Mocha to be the same as the typical done parameter. () signifies no arguments to an arrow function instead.
@chengchengpei, My best guess is that either create or destroy is not resolving, or they're taking more than 1250 milliseconds to resolve (which plus the delays of 500 and 250 milliseconds adds up to 2000).
Tangentially, the delays seem like a red flag/code smell to me. If there's something that the rest of the code (e.g. the promise chain as a whole) needs to wait to happen, chances are there should be a way to wait for it specifically instead of waiting for a specific amount of time and hoping it's happened. Whatever is specified on the promise returned from before, Mocha will wait for it to happen before starting the tests, so if it's some kind of timeout or cleanup it may need to be redesigned (the after you're already using is a good place for cleanup, for a timeout... depends what the timeout is supposed to accomplish -- it's not altogether clear from that code snippet alone what the intention of that promise structure is).
@ScottFreeCode What is the difference between _ and ()?
Function taking two arguments:
(a, b) => "had two parameters, a = " + a + " and b = " + b
Functions taking one argument:
(a) => "a is a variable with value: " + a
(_) => "_ is a variable with value: " + _
a => "a is a variable with value: " + a
_ => "_ is a variable with value: " + _
Function taking no (zero, 0) arguments:
() => "had no parameters, not even one named _"
You have to add this code in your package.json :-
"scripts": {
"test": " mocha --timeout 5000 /* your testing file name*/"
}
If none of the above works
you can always add this.timeout(0); to your test or suite to disable timeouts completely
ex.)
describe('Unit testing the Root Vue component', function() {
this.timeout(0);
it('should have the correct initial data values', function() {});
})
Hi, I am facing same issue, here is my code:
describe("get all subject", ()=>{
it("should return all subject",async ()=>{
const subject = await SubjectService.getSubjects();
expect(subject).to.be.an.instanceof(Array);
})
});
Failed messge: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Does your SubjectService take more than 2 seconds to respond?
Then either increase or disable the timeout for the test.
describe("get all subjects", () => {
it("should return all subjects", async () => {
this.timeout('10sec');
const subject = await SubjectService.getSubjects();
expect(subject).to.be.an.instanceof(Array);
});
});
Depending on the number of tests you have to run on the result, an alternative is to restructure the test to do the time-consuming part in before hook:
describe("get all subjects", () => {
let subject;
before(async () => {
this.timeout('10sec');
subject = await SubjectService.getSubjects();
});
after(() => {
subject = null;
});
it("test1", () => {
expect(subject).to.be.an.instanceof(Array);
});
...
it("testN", () => {
// different assertion
});
});
@plroebuck no, SubjectService take 0.46s to response. Actually I forgot to connect my DB. It resolved now.
But I didn't understand why this error received.
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Most helpful comment
You have to add this code in your package.json :-
"scripts": {
"test": " mocha --timeout 5000 /* your testing file name*/"
}