【发布时间】:2019-05-16 03:01:10
【问题描述】:
考虑测试以下简化函数
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。
【问题讨论】:
-
您能否检查第一个值是否不是调用第二个函数时应有的值,然后在调用第二个函数后再次检查它
-
干脆不要测试它是否完成,它是实现细节,或者你有一个具体的用例?
-
Answered here for
Mocha和Sinon...同样的概念也适用于使用Jest。
标签: javascript jestjs