From e0daf573683f44e8eb84c46101d0965cab001163 Mon Sep 17 00:00:00 2001 From: Bryan Jensen Date: Thu, 31 Aug 2023 15:35:47 -0700 Subject: [PATCH] Fix bug with inline snapshots when snapshot is string not template literal --- CHANGELOG.md | 2 ++ packages/jest-snapshot/src/InlineSnapshots.ts | 2 +- .../src/__tests__/InlineSnapshots.test.ts | 20 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7c34ab955b6..cbf606421668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 2864a8c8bce6..2fc26394ee03 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -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; diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index f40d40f5c74e..67541646894d 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -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', + ); +});