考虑测试以下简化函数
const functionToBeTested = async (val) => {
await otherModule.otherFunction(val/2);
}
在我的嘲笑测试中,我想确保otherModule.otherFunction
不仅被调用,而且还需要等待。换句话说,我想编写一个测试,如果有人从otherFunction
呼叫的前面删除await
,该测试将失败。
我到目前为止有这个
test('should wait on otherFunction', () => {
await functionToBeTested(6)
expect(otherModule.otherFunction).toHaveBeenCalledWith(3);
}
但是expect(otherModule.otherFunction).toHaveBeenCalledWith(3);
检查未验证functionToBeTested
在otherFunction
上等待。
这是我想到的:
const delay = duration => new Promise(resolve => setTimeout(resolve, duration));
test('should wait on otherFunction', async () => {
let resolve;
const mockPromise = new Promise((res) => {resolve = res;});
otherModule.otherFunction.mockReturnValue(mockPromise);
const resolution = jest.fn();
functionToBeTested(6).then(resolution);
expect(otherModule.otherFunction).toHaveBeenCalledWith(3);
await delay(0);
expect(resolution).not.toHaveBeenCalled();
resolve();
await delay(0);
expect(resolution).toHaveBeenCalled();
}
所以,我嘲笑其他功能以返回一个未解决的诺言,但是我可以在测试期间随意解决它。然后,我调用要测试的功能,并在完成后给它一个回调。
我想断言它没有调用回调,但是由于承诺解决总是异步的,所以我需要添加超时0,以使承诺有机会解决。我选择使用promisif的settimeout进行此操作。
最后,我解决了模拟声明,做一个超时0(再次,确保承诺有机会调用其回调(,并断言现在的解决方案已被称为。<<<<<<<</p>
如果您无法检查otherModule.otherFunction
解决值或任何副作用,则无需测试它解决方案。
否则,在以下示例中删除await
会导致测试失败。
describe('check for side effect', () => {
let sideEffect = false;
const otherModule = {
otherFunction: x =>
new Promise(resolve => {
setTimeout(() => {
sideEffect = true;
resolve();
}, 0);
}),
};
const functionToBeTested = async val => {
await otherModule.otherFunction(val / 2);
};
test('should wait on otherFunction', async () => {
const spy = jest.spyOn(otherModule, 'otherFunction');
await expect(functionToBeTested(6)).resolves.toBeUndefined();
expect(spy).toHaveBeenCalledWith(3);
expect(sideEffect).toBe(true);
});
});
describe('check returned value', () => {
const otherModule = {
otherFunction: x =>
new Promise(resolve => {
setTimeout(() => {
resolve('hello');
}, 0);
}),
};
const functionToBeTested = async val => {
const res = await otherModule.otherFunction(val / 2);
return `*** ${res} ***`;
};
test('should wait on otherFunction', async () => {
const spy = jest.spyOn(otherModule, 'otherFunction');
const promise = functionToBeTested(6);
expect(spy).toHaveBeenCalledWith(3);
await expect(promise).resolves.toBe('*** hello ***');
});
});