Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inline snapshot indenting #9523

Merged
merged 5 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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(\`
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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
21 changes: 21 additions & 0 deletions e2e/__tests__/toMatchInlineSnapshot.test.ts
Original file line number Diff line number Diff line change
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 = `
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay, no mutation 😀


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