Skip to content

Commit fe33f3d

Browse files
authoredJul 5, 2022
Correct await-ed inline snapshot indentation (#12986)
1 parent 51fa619 commit fe33f3d

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- `[jest-changed-files]` Fix a lock-up after repeated invocations ([#12757](https://github.com/facebook/jest/issues/12757))
1010
- `[@jest/expect-utils]` Fix deep equality of ImmutableJS OrderedSets ([#12977](https://github.com/facebook/jest/pull/12977))
11+
- `[jest-snapshot]` Fix indendation of awaited inline snapshots ([#12986](https://github.com/facebook/jest/pull/12986))
1112

1213
### Chore & Maintenance
1314

‎packages/jest-snapshot/src/InlineSnapshots.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import * as path from 'path';
99
import type {PluginItem} from '@babel/core';
10-
import type {Expression, File, Program} from '@babel/types';
10+
import {Expression, File, Program, isAwaitExpression} from '@babel/types';
1111
import * as fs from 'graceful-fs';
1212
import type {
1313
CustomParser as PrettierCustomParser,
@@ -312,7 +312,7 @@ const createFormattingParser =
312312

313313
const ast = resolveAst(parsers[inferredParser](text, options));
314314
babelTraverse(ast, {
315-
CallExpression({node: {arguments: args, callee}}) {
315+
CallExpression({node: {arguments: args, callee}, parent}) {
316316
if (
317317
callee.type !== 'MemberExpression' ||
318318
callee.property.type !== 'Identifier' ||
@@ -336,13 +336,19 @@ const createFormattingParser =
336336
return;
337337
}
338338

339+
const startColumn =
340+
isAwaitExpression(parent) && parent.loc
341+
? parent.loc.start.column
342+
: callee.loc.start.column;
343+
339344
const useSpaces = !options.useTabs;
340345
snapshot = indent(
341346
snapshot,
342347
Math.ceil(
343348
useSpaces
344-
? callee.loc.start.column / (options.tabWidth ?? 1)
345-
: callee.loc.start.column / 2, // Each tab is 2 characters.
349+
? startColumn / (options.tabWidth ?? 1)
350+
: // Each tab is 2 characters.
351+
startColumn / 2,
346352
),
347353
useSpaces ? ' '.repeat(options.tabWidth ?? 1) : '\t',
348354
);

‎packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,39 @@ test('saveInlineSnapshots() does not indent empty lines', () => {
629629
' `));\n',
630630
);
631631
});
632+
633+
test('saveInlineSnapshots() indents awaited snapshots with spaces', () => {
634+
const filename = path.join(dir, 'my.test.js');
635+
fs.writeFileSync(
636+
filename,
637+
"it('is a test', async () => {\n" +
638+
" const a = Promise.resolve({a: 'a'});\n" +
639+
' await expect(a).resolves.toMatchInlineSnapshot();\n' +
640+
'});\n',
641+
);
642+
(prettier.resolveConfig.sync as jest.Mock).mockReturnValue({
643+
bracketSpacing: false,
644+
singleQuote: true,
645+
});
646+
647+
saveInlineSnapshots(
648+
[
649+
{
650+
frame: {column: 28, file: filename, line: 3} as Frame,
651+
snapshot: "\nObject {\n a: 'a'\n}\n",
652+
},
653+
],
654+
'prettier',
655+
);
656+
657+
expect(fs.readFileSync(filename, 'utf-8')).toBe(
658+
"it('is a test', async () => {\n" +
659+
" const a = Promise.resolve({a: 'a'});\n" +
660+
' await expect(a).resolves.toMatchInlineSnapshot(`\n' +
661+
' Object {\n' +
662+
" a: 'a'\n" +
663+
' }\n' +
664+
' `);\n' +
665+
'});\n',
666+
);
667+
});

0 commit comments

Comments
 (0)
Please sign in to comment.