Skip to content

Commit

Permalink
Correct await-ed inline snapshot indentation (#12986)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpokorny committed Jul 5, 2022
1 parent 51fa619 commit fe33f3d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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 ([#12986](https://github.com/facebook/jest/pull/12986))

### Chore & Maintenance

Expand Down
14 changes: 10 additions & 4 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Expand Up @@ -7,7 +7,7 @@

import * as path from 'path';
import type {PluginItem} from '@babel/core';
import type {Expression, File, Program} from '@babel/types';
import {Expression, File, Program, isAwaitExpression} from '@babel/types';
import * as fs from 'graceful-fs';
import type {
CustomParser as PrettierCustomParser,
Expand Down Expand Up @@ -312,7 +312,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' ||
Expand All @@ -336,13 +336,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',
);
Expand Down
36 changes: 36 additions & 0 deletions packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts
Expand Up @@ -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',
);
});

0 comments on commit fe33f3d

Please sign in to comment.