我使用before
钩子与try catch
块调用我的try块中的一些功能。所以before
块在每个it
之前运行。
describe('Function response', ()=> {
// this.timeout(5000); //here
let response;
before(async () => {
// this.timeout(500000); //or here
try {
response = await myFunction(argument);
} catch (err) {
assert.fail(err);//seems doesn't work
}
});
it('function response to be an array', () => {
expect(response).to.be.an('array');
});
});
我收到了这个错误
错误:超出2000毫秒的超时。对于异步测试和挂钩,确保调用“done()”;如果返回Promise,请确保它已解决。
在打开其中一个更改默认超时的注释后,当然使箭头功能成为常规时,测试按预期工作。
我想知道什么是最佳做法。也许最好更改test
脚本中的默认超时?
"test": "mocha -r ts-node/register src/**/*.spec.ts --timeout 5000
也许我没有正确处理catch
块中的错误?
最佳做法是将超时设置在所需范围:
describe('something', function() {
this.timeout(100); // sets the timeout for everything in "describe"
before(function(done) {
this.timeout(500); // sets the timeout ONLY for "before"
setTimeout(done, 450); // <= this works
});
it('should do something', function (done) {
setTimeout(done, 150); // <= this times out
});
});
describe
中所有内容的特定超时,请在describe
中设置它before
,it
等的特定超时,请将其设置为该函数