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

[Bug]: Inline snapshots are appending a new template literal instead of replacing existing string #14464

Closed
bawjensen opened this issue Aug 31, 2023 · 2 comments · Fixed by #14465

Comments

@bawjensen
Copy link
Contributor

bawjensen commented Aug 31, 2023

Version

29.6.4

Steps to reproduce

  1. Clone https://github.com/bawjensen/jest-inline-snapshot-repro
  2. pnpm install
  3. pnpm jest inline-snapshot.test.js --updateSnapshot (though I had to run pnpm jest inline-snapshot.test.js --updateSnapshot --prettierPath '' perhaps due to quirks in my path having prettier on it)
  4. Observe the diff in inline-snapshot.test.js

Expected behavior

I see

 test("a failing inline snapshot will append, not overwrite as desired", () => {
-  expect("expected string").toMatchInlineSnapshot('"incorrect string"');
+  expect("expected string").toMatchInlineSnapshot(`"expected string"`);
 });

Actual behavior

I see

 test("a failing inline snapshot will append, not overwrite as desired", () => {
-  expect("expected string").toMatchInlineSnapshot('"incorrect string"');
+  expect("expected string").toMatchInlineSnapshot('"incorrect string"', `"expected string"`);
 });

Additional context

Our single-line template literals are being rewritten with single quotes due to our usage of the quotes eslint rule, thus we run into this issue.

I believe that I've traced the issue to

({type}) => type === 'TemplateLiteral',
, and can confirm in my setup that patching this with

       const snapshotIndex = args.findIndex(
-        ({type}) => type === 'TemplateLiteral'
+        ({type}) => type === 'TemplateLiteral' || type === 'StringLiteral'
       );

fixes the issue. I'll be pushing up a PR soon with that proposed fix.

Environment

System:
    OS: macOS 13.5.1
    CPU: (10) x64 Apple M1 Max
  Binaries:
    Node: 16.17.1 - ~/src/coda/build/node/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.15.0 - ~/src/coda/build/node/bin/npm
    pnpm: 8.6.5 - ~/src/coda/build/node/bin/pnpm
  npmPackages:
    jest: ^29.6.4 => 29.6.4
@bawjensen
Copy link
Contributor Author

From what I can tell this is the same as #10164, which was closed due to inactivity but seemed like a valid issue

@github-actions
Copy link

github-actions bot commented Oct 6, 2023

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 Oct 6, 2023
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.

1 participant