node.js - 设置超时箭头函数
问题描述
我正在做一些 mocha 测试,我被要求重构我的代码,他们要求我使用箭头函数。
但现在我收到以下错误:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
这也发生在重构之前,但我使用了解决它:this.timeout(1000)
但现在这不适用于箭头函数。如何设置高于 2000ms 的超时?下面是我的测试。
describe('Test', () => {
token = 'un_assigned';
before( (done) => {
getToken('random_token', (response) => {
token = response.token;
fs.writeFileSync('./tests/e2e/helpers/token.json', JSON.stringify(response, null, 4));
done();
})
});
files.forEach(function (file) {
it('Comparando file ' + file, (done) => {
const id = file.split('./screenshots/')[1];
compare(file, id, token, function (response) {
expect(response.TestPassed).to.be.true;
done();
});
});
});
});
解决方案
使用箭头函数时未绑定测试上下文。所以你不能使用this.timeout
.
但是您可以通过这种方式在特定测试用例上设置超时:
it('Comparando file ' + file, (done) => {
...
}).timeout(1000);
推荐阅读
- python - 更改熊猫中具有共同索引的特定单元格值
- javascript - Ramda 找到两个不同的嵌套键值
- jquery - JQuery 的 .prop() 不会设置属性
- xml - 使用 XML 在哪里可以找到函数在 Eclipse 中的来源?(苹果电脑)
- c# - selenium,使用 IWebElement 定义获取字符串值
- python - 用英语写的泰卢固语评论的情感分析
- r - 通过分隔符“+”和 R 拆分列
- algorithm - 子集和的DP解的空间优化?
- javascript - 如何通过Javascript在文本框下方添加元素?
- salesforce - Salesforce Lightning:如何在 Salesforce Lightning 组件中获取当前用户的会话 ID?