Jest: Snapshot property matchers mutate passed-in object

Created on 12 Jul 2018  路  3Comments  路  Source: facebook/jest

馃悰 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
Bug Confirmed Help Wanted good first issue

Most helpful comment

Oops, duh

All 3 comments

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)

Oops, duh

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samzhang111 picture samzhang111  路  3Comments

stephenlautier picture stephenlautier  路  3Comments

kentor picture kentor  路  3Comments

hramos picture hramos  路  3Comments

ticky picture ticky  路  3Comments