gpt4 book ai didi

javascript - 全局更改默认超时或仅更改一次测试的最佳做法是什么? Mocha

转载 作者:行者123 更新时间:2023-11-30 19:41:41 24 4
gpt4 key购买 nike

我正在使用 before 钩子(Hook)和 try catch block 来调用我的 try block 中的一些功能。所以 before block 在每个 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');
});
});

我收到这个错误

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

在打开其中一条更改默认超时的评论后,当然是在将箭头功能设置为常规后,测试按预期进行。
我想知道什么是最佳实践。也许最好在 test 脚本中更改默认超时?

"test": "mocha -r ts-node/register src/**/*.spec.ts --timeout 5000

也许我没有正确处理 catch block 中的错误?

最佳答案

最佳做法是在需要的范围内设置超时:

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 中的everything 设置特定超时,请在 describe
  • 中设置它
  • 如果您需要one beforeit 等的特定超时,请在该函数中设置它

关于javascript - 全局更改默认超时或仅更改一次测试的最佳做法是什么? Mocha ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55326076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com