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)) diff --git a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap index f574679d7f1d..1abfcc0e03ad 100644 --- a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap +++ b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap @@ -118,6 +118,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..f17ccae6f51a 100644 --- a/e2e/__tests__/toMatchInlineSnapshot.test.ts +++ b/e2e/__tests__/toMatchInlineSnapshot.test.ts @@ -14,7 +14,7 @@ import runJest from '../runJest'; 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)); @@ -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,