我正在做一些摩卡测试,我被要求重构我的代码,他们要求我使用箭头函数。
但是现在我得到了以下错误:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.在重构之前也发生了这种情况,但是为了解决这个问题,我使用了:this.timeout(1000),但是现在这并不适用于箭头函数。如何设置超过2000 How的超时值?下面是我的测试。
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();
});
});
});
});发布于 2018-08-27 18:41:26
使用箭头函数时没有绑定测试上下文。所以你不能使用this.timeout。
但是,您可以通过这种方式设置特定测试用例的超时:
it('Comparando file ' + file, (done) => {
...
}).timeout(1000);发布于 2019-12-31 11:50:24
我也遇到了这个问题,并通过将我的函数转换成以下格式来解决这个问题:
it('should do the test', done => {
...
done();
}.timeout(15000);只有在我包括、、done()和timeout(15000)之后,错误才会消失(当然,15000可能比程序运行时长多少)。
注意,虽然这个快速修复对我有用,因为我不为缓慢而烦恼,但通常情况下,通过延长超时来修复这样的错误是不好的编码实践,而应该提高程序的速度。
https://stackoverflow.com/questions/52044140
复制相似问题