Skip to content

Commit

Permalink
Prevent drifting inline snapshots #8424 (#8492)
Browse files Browse the repository at this point in the history
* Prevent drifting inline snapshots #8424

Fixes #8424 by avoiding indenting inline snapshot if second
line of inline snapshot already has been indented.

* Added pull request to changelog

* Fixed review comment

Also added a short comment to the code describing its purpose.
  • Loading branch information
petternordholm authored and scotthovestadt committed Jun 7, 2019
1 parent 07c554e commit 9ac8ca7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@

- `[babel-plugin-jest-hoist]` Expand list of whitelisted globals in global mocks ([#8429](https://github.com/facebook/jest/pull/8429)
- `[jest-core]` Make watch plugin initialization errors look nice ([#8422](https://github.com/facebook/jest/pull/8422))
- `[jest-snapshot]` Prevent inline snapshots from drifting when inline snapshots are updated ([#8492](https://github.com/facebook/jest/pull/8492))

### Chore & Maintenance

Expand Down
50 changes: 50 additions & 0 deletions packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts
Expand Up @@ -236,6 +236,56 @@ test('saveInlineSnapshots() indents multi-line snapshots with spaces', () => {
);
});

test('saveInlineSnapshots() does not re-indent already indented snapshots', () => {
const filename = path.join(__dirname, 'my.test.js');
(fs.readFileSync as jest.Mock).mockImplementation(
() =>
"it('is a test', () => {\n" +
" expect({a: 'a'}).toMatchInlineSnapshot();\n" +
'});\n' +
"it('is a another test', () => {\n" +
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
' Object {\n' +
" b: 'b'\n" +
' }\n' +
' `);\n' +
'});\n',
);
(prettier.resolveConfig.sync as jest.Mock).mockReturnValue({
bracketSpacing: false,
singleQuote: true,
});

saveInlineSnapshots(
[
{
frame: {column: 20, file: filename, line: 2} as Frame,
snapshot: `\nObject {\n a: 'a'\n}\n`,
},
],
prettier,
babelTraverse,
);

expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
"it('is a test', () => {\n" +
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
' Object {\n' +
" a: 'a'\n" +
' }\n' +
' `);\n' +
'});\n' +
"it('is a another test', () => {\n" +
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
' Object {\n' +
" b: 'b'\n" +
' }\n' +
' `);\n' +
'});\n',
);
});

test('saveInlineSnapshots() indents multi-line snapshots with tabs', () => {
const filename = path.join(__dirname, 'my.test.js');
(fs.readFileSync as jest.Mock).mockImplementation(
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-snapshot/src/inline_snapshots.ts
Expand Up @@ -120,6 +120,14 @@ const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file);

const indent = (snapshot: string, numIndents: number, indentation: string) => {
const lines = snapshot.split('\n');
// Prevent re-identation of inline snapshots.
if (
lines.length >= 2 &&
lines[1].startsWith(indentation.repeat(numIndents + 1))
) {
return snapshot;
}

return lines
.map((line, index) => {
if (index === 0) {
Expand Down

0 comments on commit 9ac8ca7

Please sign in to comment.