设置超时箭头函数
Set timeout arrow functions
我正在做一些 mocha 测试,我被要求重构我的代码,他们要求我在其中使用箭头函数。
但现在我收到以下错误:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
这也发生在重构之前但为了解决它我使用了:this.timeout(1000)
但现在这不适用于箭头函数。我怎样才能将超时设置为高于 2000 毫秒?下面是我的测试。
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);
我也有这个问题,并通过将我的函数转换为这种格式来修复它:
it('should do the test', done => {
...
done();
}.timeout(15000);
错误仅在我包含 done()
和 timeout(15000)
之后才消失(当然,15000 可以是比程序长的任意数字运行时间)。
请注意,虽然这个快速修复对我有用,因为我不为缓慢所困扰,但作为一般规则,通过延长超时来修复此类错误是一种糟糕的编码实践,而应该提高程序速度。
我正在做一些 mocha 测试,我被要求重构我的代码,他们要求我在其中使用箭头函数。
但现在我收到以下错误:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
这也发生在重构之前但为了解决它我使用了:this.timeout(1000)
但现在这不适用于箭头函数。我怎样才能将超时设置为高于 2000 毫秒?下面是我的测试。
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);
我也有这个问题,并通过将我的函数转换为这种格式来修复它:
it('should do the test', done => {
...
done();
}.timeout(15000);
错误仅在我包含 done()
和 timeout(15000)
之后才消失(当然,15000 可以是比程序长的任意数字运行时间)。
请注意,虽然这个快速修复对我有用,因为我不为缓慢所困扰,但作为一般规则,通过延长超时来修复此类错误是一种糟糕的编码实践,而应该提高程序速度。