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

fix(jest-snapshot): always run prettier format a second time #11560

Merged
merged 4 commits into from Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@

### Fixes

- `[jest-snapshot]` Correctly indent inline snapshots ([#11560](https://github.com/facebook/jest/pull/11560))

### Chore & Maintenance

### Performance
Expand Down
14 changes: 14 additions & 0 deletions e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap
Expand Up @@ -118,6 +118,20 @@ test('handles property matchers', () => {

`;

exports[`indentation is correct in the presences of existing snapshots, when the file is correctly formatted by prettier: existing snapshot 1`] = `
it('is true', () => {
expect(true).toMatchInlineSnapshot(\`true\`);
expect([1, 2, 3]).toMatchInlineSnapshot(\`
Array [
1,
2,
3,
]
\`);
});

`;

exports[`indentation is correct in the presences of existing snapshots: existing snapshot 1`] = `
test('existing snapshot', () => {
expect({hello: 'world'}).toMatchInlineSnapshot(\`
Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/toMatchInlineSnapshot.test.ts
Expand Up @@ -382,3 +382,20 @@ test('indentation is correct in the presences of existing snapshots', () => {
expect(exitCode).toBe(0);
expect(wrap(fileAfter)).toMatchSnapshot('existing snapshot');
});

test('indentation is correct in the presences of existing snapshots, when the file is correctly formatted by prettier', () => {
const filename = 'existing-snapshot.test.js';
const test = `
it('is true', () => {
expect(true).toMatchInlineSnapshot(\`true\`);
expect([1, 2, 3]).toMatchInlineSnapshot();
});\\n
`;

writeFiles(TESTS_DIR, {[filename]: test});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
const fileAfter = readFile(filename);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(exitCode).toBe(0);
expect(wrap(fileAfter)).toMatchSnapshot('existing snapshot');
});
23 changes: 10 additions & 13 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Expand Up @@ -285,22 +285,19 @@ const runPrettier = (
// Snapshots have now been inserted. Run prettier to make sure that the code is
// formatted, except snapshot indentation. Snapshots cannot be formatted until
// after the initial format because we don't know where the call expression
// will be placed (specifically its indentation).
let newSourceFile = prettier.format(sourceFileWithSnapshots, {
...config,
filepath: sourceFilePath,
});

if (newSourceFile !== sourceFileWithSnapshots) {
// prettier moved things around, run it again to fix snapshot indentations.
newSourceFile = prettier.format(newSourceFile, {
// will be placed (specifically its indentation), so we have to do two
// prettier.format calls back-to-back.
return prettier.format(
prettier.format(sourceFileWithSnapshots, {
...config,
filepath: sourceFilePath,
}),
{
...config,
filepath: sourceFilePath,
parser: createFormattingParser(snapshotMatcherNames, inferredParser),
});
}

return newSourceFile;
},
);
};

// This parser formats snapshots to the correct indentation.
Expand Down