nested objectContaining does not show diff
See original GitHub issue🐛 Bug Report
Using expect.objectContaining
in nested in another objectContaining
will not trigger a diff.
To Reproduce
expect({ data: [{ a: 1 }, { a: 2 }] }).toEqual(
expect.objectContaining({
data: [
expect.objectContaining({ a: 2 }),
expect.objectContaining({ a: 2 }),
],
})
);
expect({ a: 1, b: { c: 'c' }, c: 'd' }).toEqual(expect.objectContaining({
a: 3,
b: expect.objectContaining({ c: 'd' }),
c: expect.stringMatching('c'),
}));
Expected behavior
I would expect to see a diff showing why the objects are different. Note that this already happens when we use just one level of objectContaining
inside an array (see tests: objectContaining within array
and 'objectContaining in array
)
Link to repl or repo (highly encouraged)
https://repl.it/repls/CooperativeRipeBruteforceprogramming#nested.test.js
envinfo
System:
OS: macOS 10.15.5
CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
Binaries:
Node: 14.4.0 - /usr/local/bin/node
npm: 6.14.5 - /usr/local/bin/npm
npmPackages:
jest: ^26.1.0 => 26.1.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:5
Top Results From Across the Web
javascript - What's the difference between '.toMatchObject' and ...
From looking at the docs, and my own experimentation to confirm it, the difference is in the handling of objects nested within the...
Read more >Jest Array/Object partial match with objectContaining and ...
It's possible to do partial matches on Arrays and Objects in Jest using expect.objectContaining and expect.arrayContaining .</
Read more >Expect - Jest
When Jest is called with the --expand flag, this.expand can be used to determine if Jest is expected to show full diffs and...
Read more >TIL: You can watch for nested properties changing in React's ...
You can use nested properties in React's useEffect() dependency array. ... updates just their age, and not when any other value changes?
Read more >Nested field type | Elasticsearch Guide [8.5] | Elastic
How arrays of objects are flattenededit. Elasticsearch has no concept of inner objects. Therefore, it flattens object hierarchies into a simple list of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe this is only the case when you pass an asymmetric matcher function into toEqual. When passing an object that has nested objectContainings within it, I receive a diff output.
Looking at the source jest-matcher-utils contains a function
isLineDiffable
and if the expected or actual object is an asymmetric matcher function it returns false@SamLee is there another way to write it?
toMatchObject
requires all object properties to be defined if an object is an outer level.If I use
expect(someArr).toCountainEqual(expect.objectContaining({...}))
, I still don’t get the diff either.