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
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
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With