Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct await-ed inline snapshot indentation #12986

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
19 changes: 15 additions & 4 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Expand Up @@ -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,
SimenB marked this conversation as resolved.
Show resolved Hide resolved
isAwaitExpression,
} from '@babel/types';
import * as fs from 'graceful-fs';
import type {
CustomParser as PrettierCustomParser,
Expand Down Expand Up @@ -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' ||
Expand All @@ -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',
);
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',
);
});