Skip to content

Commit

Permalink
Fix inline snapshot indenting (#9523)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Feb 6, 2020
1 parent 2bc336b commit 02faea6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))

Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap
Expand Up @@ -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({
Expand Down
23 changes: 22 additions & 1 deletion e2e/__tests__/toMatchInlineSnapshot.test.ts
Expand Up @@ -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));
Expand Down Expand Up @@ -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');
});
4 changes: 1 addition & 3 deletions packages/jest-snapshot/src/inline_snapshots.ts
Expand Up @@ -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
) {
Expand All @@ -282,8 +282,6 @@ const createFormattingParser = (
return;
}

snapshotMatcherNames.shift();

const useSpaces = !options.useTabs;
snapshot = indent(
snapshot,
Expand Down

0 comments on commit 02faea6

Please sign in to comment.