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: strip added indent in error inline snapshots #10217

Merged
merged 4 commits into from
Jul 3, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[expect]` Match symbols and bigints in `any()` ([#10223](https://github.com/facebook/jest/pull/10223))
- `[jest-snapshot]` Strip added indentation for inline error snapshots ([#10217](https://github.com/facebook/jest/pull/10217))

### Chore & Maintenance

Expand Down
56 changes: 54 additions & 2 deletions packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,58 @@ test('saveInlineSnapshots() indents multi-line snapshots with spaces', () => {
);
});

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

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

expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
"it('is an error test', () => {\n" +
' expect(() => {\n' +
" throw new Error(['a', 'b'].join('\\n'));\n" +
' }).toThrowErrorMatchingInlineSnapshot(`\n' +
' "a\n' +
' b"\n' +
' `);\n' +
'});\n' +
"it('is another test', () => {\n" +
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
' Object {\n' +
" a: 'a'\n" +
' }\n' +
' `);\n' +
'});\n',
);
});

test('saveInlineSnapshots() does not re-indent already indented snapshots', () => {
const filename = path.join(__dirname, 'my.test.js');
(fs.readFileSync as jest.Mock).mockImplementation(
Expand All @@ -233,7 +285,7 @@ test('saveInlineSnapshots() does not re-indent already indented snapshots', () =
" expect({a: 'a'}).toMatchInlineSnapshot();\n" +
'});\n' +
"it('is a another test', () => {\n" +
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
" expect({b: 'b'}).toMatchInlineSnapshot(`\n" +
' Object {\n' +
" b: 'b'\n" +
' }\n' +
Expand Down Expand Up @@ -266,7 +318,7 @@ test('saveInlineSnapshots() does not re-indent already indented snapshots', () =
' `);\n' +
'});\n' +
"it('is a another test', () => {\n" +
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
" expect({b: 'b'}).toMatchInlineSnapshot(`\n" +
' Object {\n' +
" b: 'b'\n" +
' }\n' +
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,10 @@ const toThrowErrorMatchingInlineSnapshot = function (
return _toThrowErrorMatchingSnapshot(
{
context: this,
inlineSnapshot,
inlineSnapshot:
inlineSnapshot !== undefined
? stripAddedIndentation(inlineSnapshot)
: undefined,
isInline: true,
matcherName,
received,
Expand Down