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鈥檒l 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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(\`
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
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 = `
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
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