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

Reusing property matchers causes incorrect matching failure #9089

Closed
smolijar opened this issue Oct 24, 2019 · 3 comments · Fixed by #9322
Closed

Reusing property matchers causes incorrect matching failure #9089

smolijar opened this issue Oct 24, 2019 · 3 comments · Fixed by #9322

Comments

@smolijar
Copy link

smolijar commented Oct 24, 2019

🐛 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:

  1. Create an object with matcher A: { createdAt: expect.any(String) }
  2. Create a matcher B using the previous matcher under two different keys: { foo: timestampMatcher, bar: timestampMatcher }
  3. 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" } }
  4. 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 
@jeysal
Copy link
Contributor

jeysal commented Nov 11, 2019

Thanks for the report, confirmed! Based on your `seenReferences remarks probably introduced by #8687

@gejimayu
Copy link
Contributor

i am working on this

@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 May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants