[Bug]: Passing asymmetric matchers highlighted in diff when using .toMatchSnapshot
#13480
Closed
Description
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.
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