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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline snapshots drift to the right #8424

Closed
mrm007 opened this issue May 6, 2019 · 16 comments 路 Fixed by #8492
Closed

Inline snapshots drift to the right #8424

mrm007 opened this issue May 6, 2019 · 16 comments 路 Fixed by #8492

Comments

@mrm007
Copy link

mrm007 commented May 6, 2019

馃挜 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 
@scotthovestadt
Copy link
Contributor

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

@mrm007
Copy link
Author

mrm007 commented May 6, 2019

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

@SimenB
Copy link
Member

SimenB commented May 6, 2019

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

@mrm007 mrm007 changed the title Inline snapshot drift to the right Inline snapshots drift to the right May 8, 2019
@mrm007
Copy link
Author

mrm007 commented May 8, 2019

@SimenB that's what I ended up doing.

@Bnaya
Copy link

Bnaya commented May 9, 2019

I have a video
jest_inline_snapshot_wtf (2)

@Bnaya
Copy link

Bnaya commented May 9, 2019

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

@fwouts
Copy link

fwouts commented May 16, 2019

FYI I see this happen with Jest@24.5 as well. Jest@24.0 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?).

@jeysal
Copy link
Contributor

jeysal commented May 16, 2019

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

@langpavel
Copy link

langpavel commented May 18, 2019

It may be regression introduced by this: #8198

@jeysal
Copy link
Contributor

jeysal commented May 18, 2019

Yeah definitely

petternordholm added a commit to petternordholm/jest that referenced this issue May 23, 2019
Fixes jestjs#8424 by avoiding indenting inline snapshot if second
line of inline snapshot already has been indented.
petternordholm added a commit to petternordholm/jest that referenced this issue May 24, 2019
Fixes jestjs#8424 by avoiding indenting inline snapshot if second
line of inline snapshot already has been indented.
petternordholm added a commit to petternordholm/jest that referenced this issue May 24, 2019
Fixes jestjs#8424 by avoiding indenting inline snapshot if second
line of inline snapshot already has been indented.
petternordholm added a commit to petternordholm/jest that referenced this issue May 24, 2019
Fixes jestjs#8424 by avoiding indenting inline snapshot if second
line of inline snapshot already has been indented.
@Bnaya
Copy link

Bnaya commented Jun 1, 2019

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

scotthovestadt pushed a commit that referenced this issue Jun 7, 2019
* Prevent drifting inline snapshots #8424

Fixes #8424 by avoiding indenting inline snapshot if second
line of inline snapshot already has been indented.

* Added pull request to changelog

* Fixed review comment

Also added a short comment to the code describing its purpose.
@Bnaya
Copy link

Bnaya commented Jul 17, 2019

Any chance to see the fix released before jest 25?

@jeysal
Copy link
Contributor

jeysal commented Jul 17, 2019

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

@benjie
Copy link

benjie commented Sep 4, 2019

(Seems this fix was released in jest 24.0.18?)

@SimenB
Copy link
Member

SimenB commented Sep 4, 2019

24.9.0 has it 馃檪

facebook-github-bot pushed a commit to facebook/react-native that referenced this issue Feb 3, 2020
Summary:
This diff regenerates all Jest inline snapshots (`expect().toMatchInlineSnapshot()`) now that we use Jest 24.9.0 which fixes a longstanding [formatting instability bug](jestjs/jest#8424).

This is strictly a formatting change.

Changelog: [Internal]

Reviewed By: cpojer

Differential Revision: D19684238

fbshipit-source-id: 77c1e5fe9d2dfef85dffdcc00056fb439d7d7f84
osdnk pushed a commit to osdnk/react-native that referenced this issue Mar 9, 2020
Summary:
This diff regenerates all Jest inline snapshots (`expect().toMatchInlineSnapshot()`) now that we use Jest 24.9.0 which fixes a longstanding [formatting instability bug](jestjs/jest#8424).

This is strictly a formatting change.

Changelog: [Internal]

Reviewed By: cpojer

Differential Revision: D19684238

fbshipit-source-id: 77c1e5fe9d2dfef85dffdcc00056fb439d7d7f84
@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
Development

Successfully merging a pull request may close this issue.

8 participants