From 9a57b39d3323819d255a71b8282318811c7d5fb1 Mon Sep 17 00:00:00 2001 From: Robin Pokorny Date: Mon, 4 Jul 2022 19:56:38 +0000 Subject: [PATCH 1/6] add failing test for awaited snapshot indentation --- .../src/__tests__/InlineSnapshots.test.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index 1d8940556379..9be98e67ff3f 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -629,3 +629,39 @@ test('saveInlineSnapshots() does not indent empty lines', () => { ' `));\n', ); }); + +test('saveInlineSnapshots() indents awaited snapshots with spaces', () => { + const filename = path.join(dir, 'my.test.js'); + fs.writeFileSync( + filename, + "it('is a test', async () => {\n" + + " const a = Promise.resolve({a: 'a'});\n" + + ' await expect(a).resolves.toMatchInlineSnapshot();\n' + + '});\n', + ); + (prettier.resolveConfig.sync as jest.Mock).mockReturnValue({ + bracketSpacing: false, + singleQuote: true, + }); + + saveInlineSnapshots( + [ + { + frame: {column: 28, file: filename, line: 3} as Frame, + snapshot: "\nObject {\n a: 'a'\n}\n", + }, + ], + 'prettier', + ); + + expect(fs.readFileSync(filename, 'utf-8')).toBe( + "it('is a test', async () => {\n" + + " const a = Promise.resolve({a: 'a'});\n" + + ' await expect(a).resolves.toMatchInlineSnapshot(`\n' + + ' Object {\n' + + " a: 'a'\n" + + ' }\n' + + ' `);\n' + + '});\n', + ); +}); From b454eaaf05006e82861837d21d33d808e23781b0 Mon Sep 17 00:00:00 2001 From: Robin Pokorny Date: Mon, 4 Jul 2022 20:12:44 +0000 Subject: [PATCH 2/6] indent awaited snapshots with spaces --- packages/jest-snapshot/src/InlineSnapshots.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 2324ae7372fc..8fa5b055fa14 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -7,7 +7,12 @@ import * as path from 'path'; import type {PluginItem} from '@babel/core'; -import type {Expression, File, Program} from '@babel/types'; +import { + type Expression, + type File, + type Program, + isAwaitExpression, +} from '@babel/types'; import * as fs from 'graceful-fs'; import type { CustomParser as PrettierCustomParser, @@ -312,7 +317,7 @@ const createFormattingParser = const ast = resolveAst(parsers[inferredParser](text, options)); babelTraverse(ast, { - CallExpression({node: {arguments: args, callee}}) { + CallExpression({node: {arguments: args, callee}, parent}) { if ( callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier' || @@ -336,13 +341,19 @@ const createFormattingParser = return; } + const startColumn = + isAwaitExpression(parent) && parent.loc + ? parent.loc.start.column + : callee.loc.start.column; + const useSpaces = !options.useTabs; snapshot = indent( snapshot, Math.ceil( useSpaces - ? callee.loc.start.column / (options.tabWidth ?? 1) - : callee.loc.start.column / 2, // Each tab is 2 characters. + ? startColumn / (options.tabWidth ?? 1) + : // Each tab is 2 characters. + startColumn / 2, ), useSpaces ? ' '.repeat(options.tabWidth ?? 1) : '\t', ); From fa8c1bf8b15da8b928882ccad5c8cf1d4e96c500 Mon Sep 17 00:00:00 2001 From: Robin Pokorny Date: Mon, 4 Jul 2022 22:26:11 +0200 Subject: [PATCH 3/6] update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 680ec6a667ce..823b24641b51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[jest-changed-files]` Fix a lock-up after repeated invocations ([#12757](https://github.com/facebook/jest/issues/12757)) - `[@jest/expect-utils]` Fix deep equality of ImmutableJS OrderedSets ([#12977](https://github.com/facebook/jest/pull/12977)) +- `[jest-snapshot]` Fix indendation of awaited inline snapshots (TBD) ### Chore & Maintenance From 60a2183e773a44b1f6f68903b1394645a8d43b3d Mon Sep 17 00:00:00 2001 From: Robin Pokorny Date: Mon, 4 Jul 2022 22:37:45 +0200 Subject: [PATCH 4/6] add PR number to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 823b24641b51..2162247a3be9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ - `[jest-changed-files]` Fix a lock-up after repeated invocations ([#12757](https://github.com/facebook/jest/issues/12757)) - `[@jest/expect-utils]` Fix deep equality of ImmutableJS OrderedSets ([#12977](https://github.com/facebook/jest/pull/12977)) -- `[jest-snapshot]` Fix indendation of awaited inline snapshots (TBD) +- `[jest-snapshot]` Fix indendation of awaited inline snapshots ([#12986](https://github.com/facebook/jest/pull/12986)) ### Chore & Maintenance From a44110f58b75f8df36b77cf821914daf5ddbedd6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 5 Jul 2022 13:08:02 +0200 Subject: [PATCH 5/6] Update packages/jest-snapshot/src/InlineSnapshots.ts --- packages/jest-snapshot/src/InlineSnapshots.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 8fa5b055fa14..b0d3146c5afc 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -8,9 +8,9 @@ import * as path from 'path'; import type {PluginItem} from '@babel/core'; import { - type Expression, - type File, - type Program, + Expression, + File, + Program, isAwaitExpression, } from '@babel/types'; import * as fs from 'graceful-fs'; From 8ccfe4e618938136876c89e214f967a0abe17497 Mon Sep 17 00:00:00 2001 From: Robin Pokorny Date: Tue, 5 Jul 2022 12:11:04 +0000 Subject: [PATCH 6/6] run prettier --- packages/jest-snapshot/src/InlineSnapshots.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index b0d3146c5afc..4c2d064da3e1 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -7,12 +7,7 @@ import * as path from 'path'; import type {PluginItem} from '@babel/core'; -import { - Expression, - File, - Program, - isAwaitExpression, -} from '@babel/types'; +import {Expression, File, Program, isAwaitExpression} from '@babel/types'; import * as fs from 'graceful-fs'; import type { CustomParser as PrettierCustomParser,