question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:5

github_iconTop GitHub Comments

2reactions
SamLeecommented, Jul 19, 2020

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

1reaction
vteivanscommented, Sep 18, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found