Skip to content

Commit

Permalink
fix: strip added indent in error inline snapshots (#10217)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmmwh committed Jul 3, 2020
1 parent 08f00e9 commit 1d8285c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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

0 comments on commit 1d8285c

Please sign in to comment.