Getting error " Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called;"
Getting error " Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called;"
我看到以下使用 mocha
和 chai
库作为测试用例。
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
这里 code
用于测试亚马逊 lambda 函数的处理程序。(目前,我没有使用 super-test
npm 模块)
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()
})
})
});
这是处理程序
module.exports.getFeaturedProducts = function (event, context, callback) {
..............
.......
mongoConnection.then( function() {
return callback(null, 'test');
}, function (error) {
return return callback(true, null);;
})
}
任何人都可以解释一下发生了什么吗
您的测试花费的时间比 Mocha 预期的要长并且超时。默认情况下,所有回调函数在 2000 毫秒后超时。您需要使用 this.timeout()
调整测试套件的超时时间。
您可以在 describe()
:
的套件级别指定此项
describe('get the foo', function () {
this.timeout(10000) // all tests in this suite get 10 seconds before timeout
// hooks & tests
})
您可以在钩子中指定它,例如 before()
:
describe('get the foo', function() {
before(function() {
this.timeout(10000) // 10 second timeout for setup
})
// tests
})
您也可以在 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
})
})
帮助将 .timeout(10000)
添加到函数 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);
});
防止这种情况的另一种方法。您只需将 Mocha
默认超时 2000
ms 修改为 10000
ms。
要在 package.json
上添加 --timeout
标志,例如:
"scripts": {
// rest of your scripts
"test": "mocha server/**/*.test.js --timeout 10000"
},
我看到以下使用 mocha
和 chai
库作为测试用例。
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
这里 code
用于测试亚马逊 lambda 函数的处理程序。(目前,我没有使用 super-test
npm 模块)
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()
})
})
});
这是处理程序
module.exports.getFeaturedProducts = function (event, context, callback) {
..............
.......
mongoConnection.then( function() {
return callback(null, 'test');
}, function (error) {
return return callback(true, null);;
})
}
任何人都可以解释一下发生了什么吗
您的测试花费的时间比 Mocha 预期的要长并且超时。默认情况下,所有回调函数在 2000 毫秒后超时。您需要使用 this.timeout()
调整测试套件的超时时间。
您可以在 describe()
:
describe('get the foo', function () {
this.timeout(10000) // all tests in this suite get 10 seconds before timeout
// hooks & tests
})
您可以在钩子中指定它,例如 before()
:
describe('get the foo', function() {
before(function() {
this.timeout(10000) // 10 second timeout for setup
})
// tests
})
您也可以在 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
})
})
帮助将 .timeout(10000)
添加到函数 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);
});
防止这种情况的另一种方法。您只需将 Mocha
默认超时 2000
ms 修改为 10000
ms。
要在 package.json
上添加 --timeout
标志,例如:
"scripts": {
// rest of your scripts
"test": "mocha server/**/*.test.js --timeout 10000"
},