Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested objectContaining does not show diff #10276

Closed
sk- opened this issue Jul 14, 2020 · 5 comments · Fixed by #12264
Closed

nested objectContaining does not show diff #10276

sk- opened this issue Jul 14, 2020 · 5 comments · Fixed by #12264

Comments

@sk-
Copy link

sk- commented Jul 14, 2020

🐛 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
@sk- sk- changed the title nested objectContaining in an Array does not show diff nested objectContaining does not show diff Jul 14, 2020
@SamLee
Copy link

SamLee commented 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

@mattvb91
Copy link

Experiencing the same issue currently. @sk- did you manage to resolve it?

@vteivans
Copy link

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

LinqLover added a commit to LinqLover/downstream-repository-mining that referenced this issue May 29, 2021
@Imran99
Copy link

Imran99 commented Sep 1, 2021

In my case when I use

expect(mySpy).toHaveBeenCalledWith(expect.objectContaining({...})

I don't get a line by line diff. I only get a massive string output of the entire object.

When I do

expect(mySpy).toHaveBeenCalledWith({... , someField: expect.objectContaining({...}})

I get a more useful line by line diff.

I don't always want to do the latter though. I might need to partial match just a specific property.

@github-actions
Copy link

github-actions bot commented Mar 3, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants