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

Snapshot property matchers mutate passed-in object #6684

Closed
SimenB opened this issue Jul 12, 2018 · 4 comments · Fixed by #6528
Closed

Snapshot property matchers mutate passed-in object #6684

SimenB opened this issue Jul 12, 2018 · 4 comments · Fixed by #6528

Comments

@SimenB
Copy link
Member

SimenB commented Jul 12, 2018

🐛 Bug Report

When using property matchers in snapshots (https://jestjs.io/docs/en/snapshot-testing#property-matchers) the matcher mutates the object.

To Reproduce

test('some test', () => {
    const object = { foo: 'bar' };

    expect(typeof object.foo).toBe('string');

    expect(object).toMatchSnapshot({ foo: expect.any(String) });

    expect(typeof object.foo).toBe('string');
});

That test will fail the last assertion.

image

Expected behavior

The passed in object should not be mutated.

Link to repl or repo (highly encouraged)

REPL is on Jest 22, but see "To reproduce"

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS Sierra 10.12.6
    CPU: x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
  Binaries:
    Node: 8.11.1 - ~/.nvm/versions/node/v8.11.1/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 6.1.0 - ~/.nvm/versions/node/v8.11.1/bin/npm
  npmPackages:
    jest: ^23.1.0 => 23.1.0
@SimenB
Copy link
Member Author

SimenB commented Jul 12, 2018

Note that this might be solved by #6528, but that might take some time to land, so opening up a separate issue.

Fix should be here: https://github.com/facebook/jest/blob/6c61baa76e366a9a7bfb351108a2d4ca97820bf9/packages/jest-snapshot/src/index.js#L147

(don't Object.assign into received)

@rickhanlonii
Copy link
Member

Oops, duh

@rickhanlonii
Copy link
Member

Yeah #6528 will solve this and has a test for it

@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 12, 2021
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