Jest: Interactive Snapshot Mode not working when snapshotName specified

Created on 25 Sep 2018  ·  5Comments  ·  Source: facebook/jest

Sorry for the long issue, but I tried to give as much insights as possible as this issue was hard to spot at first and kind of hard to repro..

🐛 Bug Report

Interactive Snapshot Mode runs endlessly if multiple snapshots with snapshotName in a test file break.
Even if I update the failing snapshots, the interactive shell goes back to the same diff error with this error printed :

New snapshot was not written. The update flag must be explicitly passed to write a new snapshot.

This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default.

To Reproduce

  1. Create a test file with this content :
// my.test.js
describe('those tests', () => {
  test('will fail', () => {
    expect({ a: 2 }).toMatchSnapshot({
      a: 2
    }, 'snaphot 1');
  });

  test('will also fail', () => {
    expect({ a: 1 }).toMatchSnapshot({
      a: 1
    }, 'snapshot 2');
  });
});
  1. run $ yarn --watch my.test.js
  2. tests pass, snapshot are written for the first time
  3. modify all asserted objects (you can just copy/paste following code):
// my.test.js
describe('those tests', () => {
  test('will fail', () => {
    expect({ a: 5 }).toMatchSnapshot({
      a: 5
    }, 'snaphot 1');
  });

  test('will also fail', () => {
    expect({ a: 4 }).toMatchSnapshot({
      a: 4
    }, 'snapshot 2');
  });
});
  1. tests break, hit i key to enter Interactive Snapshot Mode
  2. hit u key to update first failing snapshot
  3. hit u key to update last failing snapshot
  4. snapshot successfully updated :
Interactive Snapshot Result
 › 2 snapshots reviewed, 2 snapshots updated
  1. hit enter key to end Interactive Snapshot Mode
  2. shell goes back to first failing snapshot because Jest didn't actually updated snapshot on filesystem
New snapshot was not written. The update flag must be explicitly passed to write a new snapshot.

This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default.

Expected behavior

At step 10, Jest should print a success message saying that all snapshots have been written.

// expected output
 PASS  05_jest-snapshot/__tests__/my.test.js
  those tests
    ✓ will fail (1ms)
    ✓ will also fail (1ms)

 › 1 snapshot written.
Snapshot Summary
 › 1 snapshot written from 1 test suite.

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   1 written, 1 passed, 2 total
Time:        0.11s, estimated 1s
Ran all test suites related to changed files.
  • If you remove snapshot names snapshot 1 and snapshot 2 and go through the same steps above, Jest will behave correctly at step 10.
  • it seems to work if the test file my.test.js contains only one test block :
describe('those tests', () => {
  test('will fail', () => {
    expect({ a: 52 }).toMatchSnapshot({
      a: 52
    }, 'snaphot 1');
  });
});

Link to repl or repo (highly encouraged)

https://github.com/mr-wildcard/jest-snapshot-interactive-mode

Run npx envinfo --preset jest

System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  Binaries:
    Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
    Yarn: 1.9.4 - /usr/local/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v8.11.3/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0
Bug

All 5 comments

Couldn't provide a repl because of interactive mode.

A repo would be awesome, although your reproduction steps looks good!

@rickhanlonii mind taking a look?

I added a repo for reproduction steps ;) (https://github.com/mr-wildcard/jest-snapshot-interactive-mode)

Wow, really great reproduction steps @mr-wildcard, I'll take a look!

Looked into this and found that this is related to this issue: https://github.com/facebook/jest/issues/7197

I confirmed that the root cause of this is #7197 so going to close in favor of that

Thanks again for the report @mr-wildcard!

Was this page helpful?
0 / 5 - 0 ratings