Reputation: 2279
I am seeing following using mocha
and chai
library for test case.
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
here is code
for testing handler for amazon lambda function.(for now , I am not using super-test
npm module)
const expect = require('chai').expect;
const mongoose = require('mongoose');
const CONFIG_DEV_MONGODB = require('../../config/config.dev').DB_CONNECTION_URL;
describe('get the foo', () => {
let handler;
before(() => {
process.env.DEPLOYMENT_STAGE = 'test';
process.env.DB_CONNECTION_URL = CONFIG_DEV_MONGODB;
handler = require('./get');
});
after(() => {
if (mongoose.connection) {
mongoose.connection.close();
}
});
it('validate getFeaturedProducts get request with storeid',function(done){
//request has the sample data for event
let request = require('../../test/fixtures/featureProductJSON');
handler.getFeaturedProducts(request, {} , (error, result) => {
expect(error).to.be.null;
expect(result).to.be.not.null;
done()
})
})
});
Here is the handler
module.exports.getFeaturedProducts = function (event, context, callback) {
..............
.......
mongoConnection.then( function() {
return callback(null, 'test');
}, function (error) {
return return callback(true, null);;
})
}
can any one explain what is happening wne
Upvotes: 8
Views: 25536
Reputation: 99
this problem faced me and i tried to fix it by several methods : 1- try to expand the mocha timeout more than 2 seconds by doing this
"scripts": {
"test": "mocha --timeout 20000",
"start": "nodemon server.js"
},
2- if it does not solve the problem ,ensure that you make export for the main server file it could be app.js or server.js or other you name it , that is the file that contain this lines of code
app.listen(3000,(()=>{
console.log("server is listen on port 3000") })
module.exports = app;
3- if it didnt solve put the timeout inside the it block
it("Should respond with 400 if there's an error on schema",async
function () {
this.timeout(20000)
let res = await request(app)
.post("/users/signin")
.send({
"email": "[email protected]",
"password":"1234"
})
expect(res.statusCode).equal(401,res.body.message);
Upvotes: 0
Reputation: 1321
Another way of prevent this situation. You just modify the Mocha
default timeout 2000
ms to 10000
ms.
To add a --timeout
flag on your package.json
like:
"scripts": {
// rest of your scripts
"test": "mocha server/**/*.test.js --timeout 10000"
},
Upvotes: 4
Reputation: 525
Helped to add .timeout(10000)
to the end of function it()
describe('Reverse String Test', () => {
it('Compare first data', async function () {
try {
await sql.connect(config);
let request = await new sql.Request()
.query('SELECT count(*) FROM dbo.valJob');
console.log(request);
await sql.close();
} catch (err) {
console.log(err)
}
}).timeout(10000);
});
Upvotes: 11
Reputation: 19418
Your tests are taking longer than Mocha expects them to take and timing out. By default, all callback functions timeout after 2000ms. You'll need to adjust the timeout of your test suite using this.timeout()
.
You can specify this at the suite level in your describe()
:
describe('get the foo', function () {
this.timeout(10000) // all tests in this suite get 10 seconds before timeout
// hooks & tests
})
You can specify this in a hook like before()
:
describe('get the foo', function() {
before(function() {
this.timeout(10000) // 10 second timeout for setup
})
// tests
})
You can also do this at the test level in your it()
describe('get the foo', function () {
it('validate getFeaturedProducts get request with storeid', function (done) {
this.timeout(10000) // 10 second timeout only for this test
// assertions
})
})
Upvotes: 17