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

#6003: include snapshot name in error message #6015

Merged
merged 6 commits into from
Apr 18, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

* `[jest-snapshot]` [**BREAKING**] Concatenate name of test, optional snapshot
name and count ([#6015](https://github.com/facebook/jest/pull/6015))
* `[jest-runtime]` Allow for transform plugins to skip the definition process
method if createTransformer method was defined.
([#5999](https://github.com/facebook/jest/pull/5999))
Expand Down
28 changes: 27 additions & 1 deletion integration-tests/__tests__/__snapshots__/failures.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,32 @@ exports[`works with async failures 1`] = `
"
`;

exports[`works with named snapshot failures 1`] = `
"FAIL __tests__/snapshot_named.test.js
✕ failing named snapshot

● failing named snapshot

expect(value).toMatchSnapshot()

Received value does not match stored snapshot \\"failing named snapshot: snapname 1\\".

- \\"bar\\"
+ \\"foo\\"

10 |
11 | test('failing named snapshot', () => {
> 12 | expect('foo').toMatchSnapshot('snapname');
| ^
13 | });
14 |

at __tests__/snapshot_named.test.js:12:17

› 1 snapshot test failed.
"
`;

exports[`works with node assert 1`] = `
"FAIL __tests__/node_assertion_error.test.js
✕ assert
Expand Down Expand Up @@ -828,7 +854,7 @@ exports[`works with snapshot failures 1`] = `

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.
Received value does not match stored snapshot \\"failing snapshot 1\\".

- \\"bar\\"
+ \\"foo\\"
Expand Down
10 changes: 10 additions & 0 deletions integration-tests/__tests__/failures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ test('works with snapshot failures', () => {
result.substring(0, result.indexOf('Snapshot Summary')),
).toMatchSnapshot();
});

test('works with named snapshot failures', () => {
const {stderr} = runJest(dir, ['snapshot_named.test.js']);

const result = normalizeDots(extractSummary(stderr).rest);

expect(
result.substring(0, result.indexOf('Snapshot Summary')),
).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`failing named snapshot: snapname 1`] = `"bar"`;
13 changes: 13 additions & 0 deletions integration-tests/failures/__tests__/snapshot_named.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+jsinfra
*/
'use strict';

test('failing named snapshot', () => {
expect('foo').toMatchSnapshot('snapname');
});
3 changes: 3 additions & 0 deletions packages/jest-snapshot/src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export default class SnapshotState {
actual: '',
count,
expected: '',
key,
pass: true,
};
} else {
Expand All @@ -174,6 +175,7 @@ export default class SnapshotState {
actual: unescape(receivedSerialized),
count,
expected: expected ? unescape(expected) : null,
key,
pass: false,
};
} else {
Expand All @@ -182,6 +184,7 @@ export default class SnapshotState {
actual: '',
count,
expected: '',
key,
pass: true,
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-snapshot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const toMatchSnapshot = function(received: any, testName?: string) {
: currentTestName || '',
received,
);
const {count, pass} = result;
const {pass} = result;
let {actual, expected} = result;

let report;
Expand All @@ -92,7 +92,7 @@ const toMatchSnapshot = function(received: any, testName?: string) {

report = () =>
`${RECEIVED_COLOR('Received value')} does not match ` +
`${EXPECTED_COLOR('stored snapshot ' + count)}.\n\n` +
`${EXPECTED_COLOR(`stored snapshot "${result.key}"`)}.\n\n` +
Copy link
Member

Choose a reason for hiding this comment

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

destructure key?

Copy link
Author

@hroemer hroemer Apr 17, 2018

Choose a reason for hiding this comment

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

I guess my above comment got swallowed by subsequent commits.
I agree with wrapping the output in double braces, but the toggling behavior (quote or not to quote) seems a bit awkward to me. I would rather favor to have the complete snapshot key on the output:

Received value does not match stored snapshot "failing named snapshot: snapname 1".

This would speed up finding the right one in the snap file, as well.

(diffMessage ||
EXPECTED_COLOR('- ' + (expected || '')) +
'\n' +
Expand Down