Jest: Inline snapshots drift to the right

Created on 6 May 2019  路  15Comments  路  Source: facebook/jest

馃挜 Regression Report

Inline snapshots are re-indented whenever another inline snapshot in the same file is saved/updated.

Last working version

Worked up to version: 24.5

Stopped working in version: 24.6

To Reproduce

Steps to reproduce the behavior:

Given this test file and we're running in watch mode:

describe('broken', () => {
  test('1', () => {
    expect(11111).toMatchInlineSnapshot();
  });

  test('2', () => {
    expect([[1]]).toMatchInlineSnapshot();
  });
});

Upon save, both snapshots are written:

describe('broken', () => {
  test('1', () => {
    expect(11111).toMatchInlineSnapshot(`11111`);
  });

  test('2', () => {
    expect([[1]]).toMatchInlineSnapshot(`
      Array [
        Array [
          1,
        ],
      ]
    `);
  });
});

Now I update the first test (expect(10000).toMatchInlineSnapshot()) and save. The snapshots get rewritten:

describe('broken', () => {
  test('1', () => {
    expect(10000).toMatchInlineSnapshot(`10000`);
  });

  test('2', () => {
    expect([[1]]).toMatchInlineSnapshot(`
            Array [
              Array [
                1,
              ],
            ]
        `);
  });
});

Now I add another test (expect(3).toMatchInlineSnapshot()) and save:

describe('broken', () => {
  test('1', () => {
    expect(10000).toMatchInlineSnapshot(`10000`);
  });

  test('2', () => {
    expect([[1]]).toMatchInlineSnapshot(`
                  Array [
                    Array [
                      1,
                    ],
                  ]
            `);
  });

  test('3', () => {
    expect(3).toMatchInlineSnapshot(`3`);
  });
});

Notice how the snapshot for test 2 slowly drifts to the right. This can get super annoying after many test-update-save cycles.

Expected behavior

Inline snapshots should stay put.

Link to repl or repo (highly encouraged)

Repl.it only has Jest 22.1.2. I didn't find a way to upgrade it.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS Mojave 10.14.4
    CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
  Binaries:
    Node: 10.15.0 - ~/.nvm/versions/node/v10.15.0/bin/node
    Yarn: 1.15.2 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.15.0/bin/npm
  npmPackages:
    jest: ^24.6.0 => 24.7.1 
Regression

Most helpful comment

24.9.0 has it 馃檪

All 15 comments

Thanks for the report. I'll take a look at it very soon. Do you know if it happens outside of watch mode?

Yep, just tested and confirmed same thing happens with jest -u.

I recommend removing the snapshot and regenerating it - that should stop the diff. Still a bug, of course, but should fix your issue 馃檪

@SimenB that's what I ended up doing.

I have a video
jest_inline_snapshot_wtf (2)

Is there a way to tell yarn generate everything from scratch?

FYI I see this happen with [email protected] as well. [email protected] also has the same bug. I'm not sure that it's a recent regression.

I also wonder if it could be somewhat linked to Prettier and its integration into Visual Studio Code on save (could there be a race condition?).

Not related to VSCode, I don't use it but still see this happen frequently using latest Jest. Didn't have time to investigate yet though :/

It may be regression introduced by this: https://github.com/facebook/jest/pull/8198

Yeah definitely

@petternordhold,
Until your pr get merged, you can downgrade jest-snapshots using yarn resolutions to 24.5

Any chance to see the fix released before jest 25?

@Bnaya yes, we haven't landed breaking changes yet, there will we another minor before 25

(Seems this fix was released in jest 24.0.18?)

24.9.0 has it 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antho2407 picture Antho2407  路  3Comments

kentor picture kentor  路  3Comments

mmcgahan picture mmcgahan  路  3Comments

jardakotesovec picture jardakotesovec  路  3Comments

Secretmapper picture Secretmapper  路  3Comments