【发布时间】:2019-03-07 07:42:53
【问题描述】:
这是一个用 Jest (v20.0.4) 编写的测试套件。
前 3 个测试是预期行为,我的问题与 Test4 有关。
test('Test1: the list should contain 7', () => {
const data = [1, 2, 7, 9];
expect(data).toContain(7);
});
// > Passes as expected
test('Test2: the list should contain 7', () => {
const data = [1, 2, 7, 9];
expect(data).toContain(8);
});
// > Fails as expected; Expected array: [1, 2, 7, 9] To contain value: 8
test('Test3: the list should contain 7', (done) => {
function callback(data) {
expect(data).toContain(7);
done();
}
setTimeout(() => {
callback([1, 2, 7, 9]);
}, 500);
});
// > Passes as expected
test('Test4: the list should contain 7', (done) => {
function callback(data) {
expect(data).toContain(8);
done();
}
setTimeout(() => {
callback([1, 2, 7, 9]);
}, 500);
});
// > Fails with Error "Timeout - Async callback was not invoked within timeout specified"
这是我的问题:
在 Test4 中,done() 在 expect 语句之后立即被调用。
所以,即使期望语句没有通过,我猜它应该会失败并出现类似于 Test2 的错误:(Expected array: [1, 2, 7, 9] To contain value: 8)
但是,它失败并出现如上所示的超时错误,这表明从未调用过 done()。
为什么?没看懂!
有人可以指导我完成这种行为吗?
我扫描了docs,但找不到与此相关的任何内容。
【问题讨论】:
-
我相信 Jest 一旦看到一个失败的期望语句就会返回。但是关于如何在期望失败后立即调用 done() 的任何想法,而不是等待超时发生。
标签: javascript unit-testing jestjs