From afb7521853b3f02722b3b5a5f766b692c3b3dc42 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 6 Feb 2020 11:02:46 +1300 Subject: [PATCH 1/4] fix(jest-snapshot): use `includes` instead of mutating array --- .../toMatchInlineSnapshot.test.ts.snap | 16 ++++++++++++++ e2e/__tests__/toMatchInlineSnapshot.test.ts | 21 +++++++++++++++++++ .../jest-snapshot/src/inline_snapshots.ts | 4 +--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap index b2d7e3c2a5a1..0c1139dd5574 100644 --- a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap +++ b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap @@ -120,6 +120,22 @@ test('handles property matchers', () => { `; +exports[`indentation is correct in the presences of existing snapshots: existing snapshot 1`] = ` +test('existing snapshot', () => { + expect({hello: 'world'}).toMatchInlineSnapshot(\` + Object { + "hello": "world", + } + \`); + expect({hello: 'world'}).toMatchInlineSnapshot(\` + Object { + "hello": "world", + } + \`); +}); + +`; + exports[`multiple custom matchers and native matchers: multiple matchers 1`] = ` const {toMatchInlineSnapshot} = require('jest-snapshot'); expect.extend({ diff --git a/e2e/__tests__/toMatchInlineSnapshot.test.ts b/e2e/__tests__/toMatchInlineSnapshot.test.ts index a58cb03fffae..b11ac0947226 100644 --- a/e2e/__tests__/toMatchInlineSnapshot.test.ts +++ b/e2e/__tests__/toMatchInlineSnapshot.test.ts @@ -360,3 +360,24 @@ test('multiple custom matchers and native matchers', () => { expect(exitCode).toBe(0); expect(wrap(fileAfter)).toMatchSnapshot('multiple matchers'); }); + +test('indentation is correct in the presences of existing snapshots', () => { + const filename = 'existing-snapshot.test.js'; + const test = ` + test('existing snapshot', () => { + expect({ hello: 'world' }).toMatchInlineSnapshot(\` + Object { + "hello": "world", + } + \`); + expect({ hello: 'world' }).toMatchInlineSnapshot(); + }); + `; + + writeFiles(TESTS_DIR, {[filename]: test}); + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); + const fileAfter = readFile(filename); + expect(stderr).toMatch('1 snapshot written from 1 test suite.'); + expect(exitCode).toBe(0); + expect(wrap(fileAfter)).toMatchSnapshot('existing snapshot'); +}); diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index e54d62ba08c6..872a024608da 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -262,7 +262,7 @@ const createFormattingParser = ( if ( callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier' || - callee.property.name !== snapshotMatcherNames[0] || + !snapshotMatcherNames.includes(callee.property.name) || !callee.loc || callee.computed ) { @@ -282,8 +282,6 @@ const createFormattingParser = ( return; } - snapshotMatcherNames.shift(); - const useSpaces = !options.useTabs; snapshot = indent( snapshot, From 8a6466bb78a2a5358555f460dd3927aacae11e6a Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 6 Feb 2020 11:06:36 +1300 Subject: [PATCH 2/4] docs(changelog): add entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e568389dd809..cf5712d1d518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499)) - `[jest-resolve]` Fix module identity preservation with symlinks and browser field resolution ([#9511](https://github.com/facebook/jest/pull/9511)) - `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451)) +- `[jest-snapshot]` Properly indent new snapshots in the presences of existing ones ([#9523](https://github.com/facebook/jest/pull/9523)) - `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460)) - `[pretty-format]` Export `OldPlugin` type ([#9491](https://github.com/facebook/jest/pull/9491)) From 372ced390aefddb6af2d5d9e27f17b3628095504 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 6 Feb 2020 12:06:35 +1300 Subject: [PATCH 3/4] chore: use `dedent` for inline snapshot test --- .../__snapshots__/toMatchInlineSnapshot.test.ts.snap | 8 ++++---- e2e/__tests__/toMatchInlineSnapshot.test.ts | 5 +++-- package.json | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap index 0c1139dd5574..40f64659a571 100644 --- a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap +++ b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap @@ -123,10 +123,10 @@ test('handles property matchers', () => { exports[`indentation is correct in the presences of existing snapshots: existing snapshot 1`] = ` test('existing snapshot', () => { expect({hello: 'world'}).toMatchInlineSnapshot(\` - Object { - "hello": "world", - } - \`); + Object { + "hello": "world", + } + \`); expect({hello: 'world'}).toMatchInlineSnapshot(\` Object { "hello": "world", diff --git a/e2e/__tests__/toMatchInlineSnapshot.test.ts b/e2e/__tests__/toMatchInlineSnapshot.test.ts index b11ac0947226..81e01523e675 100644 --- a/e2e/__tests__/toMatchInlineSnapshot.test.ts +++ b/e2e/__tests__/toMatchInlineSnapshot.test.ts @@ -10,6 +10,7 @@ import * as path from 'path'; import {wrap} from 'jest-snapshot-serializer-raw'; import {cleanup, makeTemplate, writeFiles} from '../Utils'; import runJest from '../runJest'; +import dedent = require('dedent'); const DIR = path.resolve(__dirname, '../to-match-inline-snapshot'); const TESTS_DIR = path.resolve(DIR, '__tests__'); @@ -363,7 +364,7 @@ test('multiple custom matchers and native matchers', () => { test('indentation is correct in the presences of existing snapshots', () => { const filename = 'existing-snapshot.test.js'; - const test = ` + const test = dedent(` test('existing snapshot', () => { expect({ hello: 'world' }).toMatchInlineSnapshot(\` Object { @@ -372,7 +373,7 @@ test('indentation is correct in the presences of existing snapshots', () => { \`); expect({ hello: 'world' }).toMatchInlineSnapshot(); }); - `; + `); writeFiles(TESTS_DIR, {[filename]: test}); const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); diff --git a/package.json b/package.json index edaa90852970..44871fb8a4cc 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "chokidar": "^3.3.0", "codecov": "^3.0.0", "debug": "^4.0.1", + "dedent": "^0.7.0", "eslint": "^6.2.2", "eslint-config-prettier": "^6.1.0", "eslint-plugin-babel": "^5.1.0", From cecda64e344c3ab24d1ae98c18e53ddb783a1243 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 6 Feb 2020 02:13:55 +0100 Subject: [PATCH 4/4] remove extra dedent --- e2e/__tests__/toMatchInlineSnapshot.test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/toMatchInlineSnapshot.test.ts b/e2e/__tests__/toMatchInlineSnapshot.test.ts index 81e01523e675..f17ccae6f51a 100644 --- a/e2e/__tests__/toMatchInlineSnapshot.test.ts +++ b/e2e/__tests__/toMatchInlineSnapshot.test.ts @@ -10,12 +10,11 @@ import * as path from 'path'; import {wrap} from 'jest-snapshot-serializer-raw'; import {cleanup, makeTemplate, writeFiles} from '../Utils'; import runJest from '../runJest'; -import dedent = require('dedent'); const DIR = path.resolve(__dirname, '../to-match-inline-snapshot'); const TESTS_DIR = path.resolve(DIR, '__tests__'); -const readFile = filename => +const readFile = (filename: string) => fs.readFileSync(path.join(TESTS_DIR, filename), 'utf8'); beforeEach(() => cleanup(TESTS_DIR)); @@ -364,7 +363,7 @@ test('multiple custom matchers and native matchers', () => { test('indentation is correct in the presences of existing snapshots', () => { const filename = 'existing-snapshot.test.js'; - const test = dedent(` + const test = ` test('existing snapshot', () => { expect({ hello: 'world' }).toMatchInlineSnapshot(\` Object { @@ -373,7 +372,7 @@ test('indentation is correct in the presences of existing snapshots', () => { \`); expect({ hello: 'world' }).toMatchInlineSnapshot(); }); - `); + `; writeFiles(TESTS_DIR, {[filename]: test}); const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);