我有以下要测试的模块。
import * as apiUtils from './apiUtils';
export function awardPoints(pointsAwarding) {
return apiUtils.callApiService("POST", "points", pointsAwarding);
}
这是我的考验。
it("should call apiUtils with correct path", () => {
//given
jest.spyOn(apiUtils, 'callApiService');
//when
pointsAwardingApi.awardPoints();
//then
expect(apiUtils.callApiService).toBeCalledWith(expect.any(String), 'points', expect.any(Object));
});
当我尝试运行此测试时,出现以下错误。
Any<Object> as argument 3, but it was called with undefined.
我期望
expect.any(Object)
也可以匹配undefined
,但事实并非如此。我也尝试了
expect.anything()
,但收到类似的错误。我可以简单地将
undefined
指定为第三个参数,但是我的测试仅用于验证第二个参数。有没有一种方法可以使用匹配器匹配包括
undefined
在内的任何对象? 最佳答案
看来expect.anything
与设计上未设置的值不匹配。如果您需要“真正的一切”匹配器,请使用以下匹配器:
export const expectAnythingOrNothing = expect.toBeOneOf([expect.anything(), undefined, null]);
您将需要jest-extended
作为toBeOneOf
匹配器-但这仍然是一个有用的包:)关于javascript - 在Jest中使用 `expect.any(Object)`或 `expect.anything()`无法匹配 `undefined`,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47027011/