即使在返回承诺后承诺测试超时
Promise test timing out even after returning promise
我研究发现,在 mocha 中测试 promise 时,你必须 return promise。
我尝试执行以下操作,但测试一直超时。正确的做法是什么?
describe('A promise', () => {
it('should not timeout', () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('hi!');
}, 3000);
}).then((msg) => {
expect(msg).to.equal('hi!');
});
});
});
输出:
$ ./node_modules/mocha/bin/mocha test.js
A promise
1) should not timeout
0 passing (2s)
1 failing
1) A promise
should not timeout:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
编辑:我尝试在 it
行中添加 done
并在我的 then
块中调用它,但它没有工作
这个有用吗?
it('should not timeout', done => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('hi!');
}, 1000);
}).then((msg) => {
expect(msg).to.equal('hi!');
done();
});
});
首先需要在回调中加入done参数
it('should not timeout', (done) => {
最后调用它,
}).then((msg) => {
expect(msg).to.equal('hi!');
done()
});
您有 3 个选项:
- return一个承诺
- 使用完成(如果你也 return 承诺错误将是
抛出)
- 使用async/await
在所有这些情况下,您都必须设置超时阈值,因为 mocha 无法确定您是否会解析异步调用。这是对无尽测试的保护。
通常,你需要用一些带有一些假值的立即解决的承诺来伪造你承诺的调用,所以你不会有这样的问题。
试试这个(唯一的变化是:“.timeout(5000)”被添加到 "it")。这对我有用。基本上,您必须更改异步调用的默认超时 2 秒 - 如果您预计异步调用将花费超过 2 秒。
describe('A promise', () => {
it('should not timeout', () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('hi!');
}, 3000);
}).then((msg) => {
expect(msg).to.equal('hi!');
});
}).timeout(5000);
});
第二个选项(在这种情况下无需更改测试):
./node_modules/mocha/bin/mocha --timeout 5000 test-mocha-spec.js
我研究发现,在 mocha 中测试 promise 时,你必须 return promise。
我尝试执行以下操作,但测试一直超时。正确的做法是什么?
describe('A promise', () => {
it('should not timeout', () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('hi!');
}, 3000);
}).then((msg) => {
expect(msg).to.equal('hi!');
});
});
});
输出:
$ ./node_modules/mocha/bin/mocha test.js
A promise
1) should not timeout
0 passing (2s)
1 failing
1) A promise
should not timeout:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
编辑:我尝试在 it
行中添加 done
并在我的 then
块中调用它,但它没有工作
这个有用吗?
it('should not timeout', done => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('hi!');
}, 1000);
}).then((msg) => {
expect(msg).to.equal('hi!');
done();
});
});
首先需要在回调中加入done参数
it('should not timeout', (done) => {
最后调用它,
}).then((msg) => {
expect(msg).to.equal('hi!');
done()
});
您有 3 个选项:
- return一个承诺
- 使用完成(如果你也 return 承诺错误将是 抛出)
- 使用async/await
在所有这些情况下,您都必须设置超时阈值,因为 mocha 无法确定您是否会解析异步调用。这是对无尽测试的保护。
通常,你需要用一些带有一些假值的立即解决的承诺来伪造你承诺的调用,所以你不会有这样的问题。
试试这个(唯一的变化是:“.timeout(5000)”被添加到 "it")。这对我有用。基本上,您必须更改异步调用的默认超时 2 秒 - 如果您预计异步调用将花费超过 2 秒。
describe('A promise', () => {
it('should not timeout', () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('hi!');
}, 3000);
}).then((msg) => {
expect(msg).to.equal('hi!');
});
}).timeout(5000);
});
第二个选项(在这种情况下无需更改测试):
./node_modules/mocha/bin/mocha --timeout 5000 test-mocha-spec.js