两者之间有什么区别
it('should blabla', async () => {
await expect(callThatReturnsAPromise()).toBe(false);
});
和:
it('should blabla', async () => {
expect(await callThatReturnsAPromise()).toBe(false);
});
那么,在外部期望函数内部还是外部?
如果toBe内的调用也返回一个promise,我应该使用哪个版本呢?
it('should blabla', async () => {
await expect(callThatReturnsAPromise()).toBe(callThatReturnsAPromise2());
});
或:
it('should blabla', async () => {
expect(await callThatReturnsAPromise()).toBe(await callThatReturnsAPromise2());
});
那么等待会在期望之外解决所有承诺吗?还是我必须在函数内部等待每个承诺?
好的,我用以下代码进行了测试:
let a = function() {
console.log('huba');
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(42);
}, 5000);
});
};
let b = function() {
console.log('buba');
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(11);
}, 5000);
});
};
describe('test', function() {
it('blabla', async function() {
await expect(a()).toBe(b());
});
});
,并且期望之外的等待似乎在等待诺言a()和b()的解决。但是我不知道为什么。期望既不会带来诺言也不会实现。那我为什么要在他们外面等着呢?