Jest setTimeout not pausing test
1 2 3 4 5 | it('has working hooks', async () => { setTimeout(() => { console.log("Why don't I run?") expect(true).toBe(true) }, 15000) |
我已经查看了此答案,Jest文档和几个GitHub线程:
禁用Jest setTimeout模拟
现在,超时内的函数无法运行。
如何使Jest暂停执行测试15秒钟,然后运行内部函数?
谢谢!
1 2 3 4 5 6 7 | it('has working hooks', async () => { await new Promise(res => setTimeout(() => { console.log("Why don't I run?") expect(true).toBe(true) res() }, 15000)) }) |
或
1 2 3 4 5 6 7 | it('has working hooks', done => { setTimeout(() => { console.log("Why don't I run?") expect(true).toBe(true) done() }, 15000) }) |
一种不错且干净的方法(没有回调),我们可以简单地运行
1 2 3 4 5 6 7 | it('works with a setTimeout', async () => { // await 15000ms before continuing further await new Promise(res => setTimeout(res, 15000)); // run your test expect(true).toBe(true) }); |