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

Fix bug with inline snapshots when snapshot is string not template literal #14465

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-snapshot]` Allow for strings as well as template literals in inline snapshots ([#14465](https://github.com/jestjs/jest/pull/14465))

### Performance

- `[@jest/create-cache-key-function]` Cache access of `NODE_ENV` and `BABEL_ENV` ([#14455](https://github.com/jestjs/jest/pull/14455))
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/InlineSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ const traverseAst = (
snapshotMatcherNames.push(callee.property.name);

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

const {snapshot} = inlineSnapshot;
Expand Down
20 changes: 20 additions & 0 deletions packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,23 @@ test('saveInlineSnapshots() prioritize parser from project/editor configuration'
'});\n',
);
});

test('saveInlineSnapshots() replaces string literal, not just template literal', () => {
const filename = path.join(dir, 'my.test.js');
fs.writeFileSync(filename, 'expect("a").toMatchInlineSnapshot("b");\n');

saveInlineSnapshots(
[
{
frame: {column: 13, file: filename, line: 1} as Frame,
snapshot: 'a',
},
],
dir,
'prettier',
);

expect(fs.readFileSync(filename, 'utf-8')).toBe(
'expect("a").toMatchInlineSnapshot(`a`);\n',
);
});