mCY*_*mCY 5 mocha.js async-await
我想用 测试异步代码Mocha。
我按照本教程testing-promises-with-mocha进行操作。最后,它说最好的方法是 async/await。
以下是我的代码,我打算将 setTimeout 设置得比 Mocha 默认值长。
describe('features', () => {
it('assertion success', async() => {
const resolvingPromise = new Promise( resolve => {
setTimeout(() => {
resolve('promise resolved')
}, 3000)
})
const result = await resolvingPromise
expect(result).to.equal('promise resolved')
})
})
Run Code Online (Sandbox Code Playgroud)
摩卡给我错误如下:
Error: Timeout of 2000ms exceeded. For async tests and hooks,
ensure "done()" is called; if returning a Promise, ensure it resolves...
Run Code Online (Sandbox Code Playgroud)
如何解决该错误?简单设置mocha --timeout 10000更长?
谢谢你的时间!
Mocha: 5.2.0
Chai: 4.2.0
Run Code Online (Sandbox Code Playgroud)
Dre*_*ese -2
Mocha 有超时控制。摩卡文档
语法是:
it('should take less than 500ms', function(done) {
this.timeout(5000); // 5 seconds
setTimeout(done, 3000);
});
Run Code Online (Sandbox Code Playgroud)
你的例子:
describe('features', () => {
it('assertion success', async (done) => {
this.timeout(5000);
const resolvingPromise = new Promise(resolve => {
setTimeout(() => resolve('promise resolved'), 3000)
});
const result = await resolvingPromise();
expect(result).to.equal('promise resolved');
done();
});
});
Run Code Online (Sandbox Code Playgroud)