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

[Bug]: Passing asymmetric matchers highlighted in diff when using .toMatchSnapshot #13480

Closed
hthorhalls opened this issue Oct 20, 2022 · 4 comments · Fixed by #13491
Closed

Comments

@hthorhalls
Copy link
Contributor

Version

29.2.1

Steps to reproduce

Run jest on the following test file:

it('test snapshot bug', () => {
    const user = {
      createdAt: new Date(),
      id: Math.floor(Math.random() * 20),
      name: "Lebron James",
      some_boolean: true

    };
  
    expect(user).toMatchSnapshot({
      createdAt: expect.any(Date),
      id: expect.any(Number),
      name: expect.stringMatching("Lebron Jaames")
    });
  });

Expected behavior

I expect only the name field to be highlighted in the diff.

Actual behavior

All the property matchers are highlighted and the diff explicitly mentions three lines as being different.
Screen Shot 2022-10-20 at 5 55 40 PM

Additional context

This is a huge problem when you are testing against a big API payload and you have dozens of property matchers uisng regexes etc. It can be very hard to find the culprit field. It sounded like #9257 would fix this but the toMatchSnapshot code never executes printDiffOrStringify to my knowledge.

Environment

System:
    OS: macOS 12.5.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 16.18.0 - ~/.nvm/versions/node/v16.18.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.18.0/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v16.18.0/bin/npm
  npmPackages:
    jest: ^29.2.1 => 29.2.1
@hthorhalls hthorhalls changed the title [Bug]: Passing asymmetric matchers highlighted in diff when using .toMatchSnapshot [Bug]: Passing asymmetric matchers highlighted in diff when using .toMatchSnapshot Oct 20, 2022
@SimenB
Copy link
Member

SimenB commented Oct 21, 2022

Wanna send a PR for it? 🙂

@hthorhalls
Copy link
Contributor Author

@SimenB I took a crack at it #13491

hthorhalls added a commit to hthorhalls/jest that referenced this issue Oct 24, 2022
@SimenB
Copy link
Member

SimenB commented Oct 24, 2022

@github-actions
Copy link

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 Nov 24, 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.

2 participants