When I try to use promises with mocha, I'm getting the error below:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Actually, I'm trying to retireve data from a remote server. Mocha won't wait until the promise has been resolved or rejected.
function fn(){
return new Promise((resolve, reject) => {
setTimeout(() => resolve({foo:'bar'}),7000);
})
}
it('should retrieve data',function(done){
fn().then((res) => done()).catch((err) => done(err));
});
After 2 seconds, this error occurs.
I've been planning to check the returning data within then and call the assert.
it('should retrieve data',function(done){
fn().then(() => {
assert(true)
done()
});
});
What am I doing wrong? I want to check the returning data if it has proper keys, etc.
If it is expected/acceptable for your test to take longer than the timeout specified in the error message, you can override the amount of time for the timeout using this.timeout(<maximum acceptable time>) or --timeout <maximum acceptable time>.
P.S. You can remove done from the tests and return fn().then(... instead, Mocha will treat promise resolution as success and rejection as failure.
chai-http and expectError: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
I was planning to run the check if function called is function or not and have not used _chai-http_ any where within test .
Code :--
const app = require('../../app');
const chai = require('chai');
const expect = require('chai').expect;
const chaiHttp = require('chai-http');
const async = require('async');
const sellerPurchase = require('../../lib/seller/purchases');
chai.use(chaiHttp);
describe('SELLER: PURCHASE module', () => {
describe('"#insertNewOrder()"', () => {
// 1. Checks whether the variable exported from that file is a function itself or not
it('should be a function', done => {
this.timeout(10000);
expect(sellerPurchase.insertNewOrder).to.be.a('function');
done();
});
});
});
Now the thing is when I am not loading server file app or const app = = require('../../app'); this is working fine but when i am loading it is giving the error.
Note :-- I have tried increasing timeout still its not working.
@Jo-ravar I can't really debug that since I don't have your ../../app or ../../lib/seller/purchases. Do you think you could trim down those two modules to the simplest they can be that will still cause the issue, and then post the simplified version? (There are no obvious problems with the code you have posted. In fact, given that it's a synchronous test and all it does is have an assertion check that a property is a function, you'd have to abuse some relatively advanced/hacky JavaScript techniques to slow it down at all, let alone cause it to time out. But the only way to figure out for sure is to have all the code needed to run it and get the issue.)
const app = require('../../app');
const chai = require('chai');
const expect = require('chai').expect;
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
describe('"#Check Sum"', function() {
this.timeout(10000);
// 1. Checks whether the sum is number or not
it('should be a number', done => {
expect(2 + 3).to.be.a('number');
done();
});
});
The only thing causing issue is the first line here whenever I am loading the server or app.js file it is giving error and when I am not loading it is working fine.
Note :---
I'm still not seeing the code for the app.js file?
@Jo-ravar How much timeout did you set? I was testing one upload function, and it depended on size of file I was trying to upload. So, sometimes it didn't give me error on 10 seconds, while for some uploads it even gave me failures for 45 second timeout.
@Jo-ravar i am too getting the same error , have you found the solution?
Any resolution on this?
This needs an mcve; I don't believe it's a Mocha problem anyway. Re-open if someone can confirm.
I have found the solution for my case here #2025
For the people who still struggling with this problem, just add this.enableTimeouts(false) may solve it. Example:
describe('Test', function () {
this.enableTimeouts(false)
// your code here
}
You can may also try to run the code last. I had the same issue but was able to by pass it using:
after('Test', (done) => {
//your code
});
go here https://mochajs.org/#timeouts
Hey, guys, I was running through the same error. after carefully searching I found out I need to call the "done()" function. It was given some trouble but now it passes the test.
describe('sample_tests', function () {
// this.timeout(10000)
it('test 0', (done) => {
expect(1).to.be.equal(1);
---> done();
});
});
:neckbeard: Other option, you can use de TIMEOUTS suite, check this example:
describe('a suite of 7 seconds', function () {
this.timeout(7500);
it('should take less than 5s', function (done) {
setTimeout(done, 5000);
});
it('should take less than 2s as well', function (done) {
setTimeout(done, 2000);
});
});
More info: :point_right: https://mochajs.org/#timeouts
Most helpful comment
For the people who still struggling with this problem, just add
this.enableTimeouts(false)may solve it. Example: