我看到以下内容使用mocha
和chai
库进行测试用例。 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
以下是测试Amazon Lambda函数处理程序code
。(目前,我没有使用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);;
})
}
谁能解释一下WNE正在发生的事情
您的测试花费的时间比 Mocha 预期的要长,并且超时。默认情况下,所有回调函数在 2000ms 后超时。您需要使用 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
毫秒修改为 10000
毫秒。要在package.json
上添加--timeout
标志,如下所示:
"scripts": {
// rest of your scripts
"test": "mocha server/**/*.test.js --timeout 10000"
},
我,我试图通过几种方法来解决它:1-尝试通过执行此操作将摩卡超时延长2秒以上
"scripts": {
"test": "mocha --timeout 20000",
"start": "nodemon server.js"
},
2-如果它不能解决问题,请确保为主服务器文件导出它可能是app.js或服务器.js或者其他你命名的,即包含这行代码
的文件 app.listen(3000,(()=>{
console.log("server is listen on port 3000") })
module.exports = app;
3-如果没有解决,则将超时放在IT块中
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": "ahmed22kjsdnkjdsbf@yahoo.com",
"password":"1234"
})
expect(res.statusCode).equal(401,res.body.message);