Skip to content

Commit

Permalink
Fix bug with inline snapshots when snapshot is string not template li…
Browse files Browse the repository at this point in the history
…teral
  • Loading branch information
bawjensen committed Aug 31, 2023
1 parent f2c78d0 commit e0daf57
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
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

### 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',
);
});

0 comments on commit e0daf57

Please sign in to comment.