我正在使用节点v9.4.0运行。在我的测试套件中,如何使用箭头符号设置超时。下面的
describe('Test Stratum client[callbacks]', () => { this.timeout(5000); // Test for onConnect getting called it('onConnect', (done) => {
是导致错误的原因
_this.timeout(5000); ^ TypeError: _this.timeout is not a function
在不更改“描述”行的情况下,我可以使用什么语法来设置超时?
您可以将其余部分封装在函数中并使用setTimeout
describe('Test Stratum client[callbacks]', () => { setTimeout(() => { // Test for onConnect getting called it('onConnect', (done) => { //The rest of your code, etc etc }); //blah blah blah }, 5000);
替代方法
节点中没有超时。js或JavaScript。但是,如果您在异步上下文中工作,则可以使用变通方法:
const timeout = ms => new Promise(res => setTimeout(res, ms)); describe('Test Stratum client[callbacks]', async () => { await timeout(5000); //etc etc
请注意 async 之前 () => { 在第二行
async
() => {