最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest exact object matching - Stack Overflow

programmeradmin13浏览0评论

Is there a function with a similar API to expect.toMatchObject(), but that will throw errors if the expected and received don't have the exact same set of keys, i.e a function toExactMatchObject such as

expect({a: 1, b: 2, c:3}).not.toExactMatchObject({a: expect.any(Number), b: expect.any(Number)}) // -> Should pass

This seems like a very frequent use-case, yet I can't find anything in the official documentation.

Is there a function with a similar API to expect.toMatchObject(), but that will throw errors if the expected and received don't have the exact same set of keys, i.e a function toExactMatchObject such as

expect({a: 1, b: 2, c:3}).not.toExactMatchObject({a: expect.any(Number), b: expect.any(Number)}) // -> Should pass

This seems like a very frequent use-case, yet I can't find anything in the official documentation.

Share Improve this question edited Jul 8, 2021 at 7:03 Lin Du 103k136 gold badges334 silver badges567 bronze badges asked Jul 2, 2021 at 3:51 Bertrand CaronBertrand Caron 2,6772 gold badges27 silver badges49 bronze badges 1
  • @slideshowp2 That appears to work. Do you want to post it as an answer and I'll accept it? I still think there is value in my question for the way it is phrased. – Bertrand Caron Commented Jul 8, 2021 at 6:56
Add a ment  | 

1 Answer 1

Reset to default 7

The imaginary toExactMatchObject should be equivalent to .toEqual(value) Matcher

Use .toEqual to pare recursively all properties of object instances (also known as "deep" equality).

expect({ a: 1, b: 2, c: 3 }).not.toEqual({ a: expect.any(Number), b: expect.any(Number) })
发布评论

评论列表(0)

  1. 暂无评论