From b01f65215ff5f344ef5af209d769de7477562607 Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Mon, 29 Jun 2020 01:54:57 +0800 Subject: [PATCH 1/4] fix: strip added indent in error inline snapshots --- packages/jest-snapshot/src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 2fed614d7d53..ef7b407fdf85 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -462,7 +462,10 @@ const toThrowErrorMatchingInlineSnapshot = function ( return _toThrowErrorMatchingSnapshot( { context: this, - inlineSnapshot, + inlineSnapshot: + inlineSnapshot !== undefined + ? stripAddedIndentation(inlineSnapshot) + : undefined, isInline: true, matcherName, received, From 6339834ac7646d52fdc2eb05490a2150e0dd607d Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Mon, 29 Jun 2020 02:03:28 +0800 Subject: [PATCH 2/4] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ed6626fd036..6075c2ceb399 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From fe0e701c1fdc9a53de534aed2a5ecae7cdc1b598 Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Fri, 3 Jul 2020 00:05:21 +0800 Subject: [PATCH 3/4] test: add tests for error inline snapshot re-indent --- .../src/__tests__/inline_snapshots.test.ts | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index b605a863eff1..aafde7b00958 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -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( @@ -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' + @@ -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' + From 3c8f7f990b5cb6d7f73e623b37e41d8549c73a90 Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Fri, 3 Jul 2020 00:34:03 +0800 Subject: [PATCH 4/4] chore: lint with prettier --- .../src/__tests__/inline_snapshots.test.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index aafde7b00958..6edd9ad819ca 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -260,20 +260,20 @@ test('saveInlineSnapshots() does not re-indent error snapshots', () => { 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', + ' 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', ); });