[英]How to strictly test objectContaining with jest?
有沒有辦法用玩笑嚴格測試.objectConaining
?
基本上,下面的代碼檢查其中一個 object 鍵,並且測試通過,但我不希望它通過。
是否有某種方法可以嚴格測試此 object,例如.toStrictEquals
以便測試不僅會檢查特定鍵,還會檢查 object 結構?
it('should work', () => {
const spy = jest.fn();
const obj = {
hello: "world",
foo: "bar",
}
spy(obj);
expect(spy).toBeCalledWith( expect.objectContaining({
hello: expect.stringContaining("world")
}));
});
基本上我的目標是嚴格檢查obj
。
你可以這樣做:
it('should work', () => {
const obj = {
hello: "world",
foo: "bar",
}
expect(obj).toEqual({
hello: "world"
})
});
這將打印以下內容:
expect(received).toEqual(expected) // deep equality
- Expected - 0
+ Received + 1
Object {
+ "foo": "bar",
"hello": "world",
}
5 | }
6 |
> 7 | expect(obj).toEqual({
| ^
8 | hello: "world"
9 | })
10 | });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.