Closed
Description
🐛 Bug Report
Reusing the same objects with property matchers results in unsuccessful validation of the object towards it.
To Reproduce
Steps to reproduce the behavior:
- Create an object with matcher A:
{ createdAt: expect.any(String) }
- Create a matcher B using the previous matcher under two different keys:
{ foo: timestampMatcher, bar: timestampMatcher }
- Create a value that satisfies the matcher B, but has additional values for one one of the values for matcher A:
{ foo: { createdAt: "def" }, bar: { createdAt: "def", baz: "ab1" } }
- Test
expect(value).toMatchSnapshot(B);
Expected behavior
I expected to successfully create snapshots, because value satisfy the property matcher constrains.
Instead I get the follwing error.
● Matches property matchers
expect(received).toMatchSnapshot(properties)
Snapshot name: `Matches property matchers 1`
Expected properties: {"bar": {"createdAt": Any<String>}, "foo": {"createdAt": Any<String>}}
Received value: {"bar": {"baz": "ab1", "createdAt": "def"}, "foo": {"createdAt": "def"}}
Debug info
Creating new matcher A for each property of B ({ foo: {...timestampMatcher}, bar: {...timestampMatcher} }
) does not trigger the error and works as expected. (shown in MWE)
I tried to debug it and found out that it is caused by seenReferences
"caching" in expect
. Disabling this if block fixes the issue.
Alas I could not find a way to fix it without disabling the seenRefences feature.
Link to repl or repo (highly encouraged)
https://github.com/grissius/jest-bug-reuse-property-matchers
envinfo
System:
OS: Linux 4.19 Manjaro Linux undefined
CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
Binaries:
Node: 12.11.1 - ~/.nvm/versions/node/v12.11.1/bin/node
Yarn: 1.19.1 - /usr/bin/yarn
npm: 6.11.3 - ~/.nvm/versions/node/v12.11.1/bin/npm
npmPackages:
jest: ^24.9.0 => 24.9.0