我看到使用mocha
和chai
库进行测试用例。
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
这里是code
用于测试amazon 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);;
})
}
任何人都可以解释发生了什么
答案 0 :(得分:2)
你的测试花费的时间比Mocha预计的更长,超时。默认情况下,所有回调函数在2000ms后超时。您需要使用this.timeout()
调整测试套件的超时。
您可以在,v
:
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()
答案 1 :(得分:0)
帮助将.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);
});
答案 2 :(得分:0)
防止这种情况的另一种方法。您只需将Mocha
默认超时2000
ms修改为10000
ms。
要在--timeout
上添加package.json
标志,例如:
"scripts": {
// rest of your scripts
"test": "mocha server/**/*.test.js --timeout 10000"
},