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
1 Answer
Reset to default 7The 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) })