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

Inline snapshots: do not indent empty lines. #8277

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-snapshot]` Inline snapshots: do not indent empty lines ([#8277](https://github.com/facebook/jest/pull/8277))

### Chore & Maintenance

### Performance
Expand Down
33 changes: 33 additions & 0 deletions packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,36 @@ test('saveInlineSnapshots() indents snapshots after prettier reformats', () => {
' `));\n',
);
});

test('saveInlineSnapshots() does not indent empty lines', () => {
const filename = path.join(__dirname, 'my.test.js');
(fs.readFileSync as jest.Mock).mockImplementation(
() =>
"it('is a test', () => expect(`hello\n\nworld`).toMatchInlineSnapshot());\n",
);
(prettier.resolveConfig.sync as jest.Mock).mockReturnValue({
bracketSpacing: false,
singleQuote: true,
});

saveInlineSnapshots(
[
{
frame: {column: 9, file: filename, line: 3} as Frame,
snapshot: `\nhello\n\nworld\n`,
},
],
prettier,
babelTraverse,
);

expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
"it('is a test', () =>\n" +
' expect(`hello\n\nworld`).toMatchInlineSnapshot(`\n' +
' hello\n' +
'\n' +
' world\n' +
' `));\n',
);
});
15 changes: 9 additions & 6 deletions packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ function stripAddedIndentation(inlineSnapshot: string) {
}

for (let i = 1; i < lines.length - 1; i++) {
if (lines[i].indexOf(indentation) !== 0) {
// All lines except first and last should have the same indent as the
// first line (or more). If this isn't the case we don't want to touch it.
return inlineSnapshot;
}
if (lines[i] !== '') {
if (lines[i].indexOf(indentation) !== 0) {
// All lines except first and last should either be blank or have the same
// indent as the first line (or more). If this isn't the case we don't
// want to touch the snapshot at all.
return inlineSnapshot;
}

lines[i] = lines[i].substr(indentation.length);
lines[i] = lines[i].substr(indentation.length);
}
}

// Last line is a special case because it won't have the same indent as others
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-snapshot/src/inline_snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ const indent = (snapshot: string, numIndents: number, indentation: string) => {
// First line is either a 1-line snapshot or a blank line.
return line;
} else if (index !== lines.length - 1) {
// Do not indent empty lines.
if (line === '') {
return line;
}

// Not last line, indent one level deeper than expect call.
return indentation.repeat(numIndents + 1) + line;
} else {
Expand Down