Skip to content

Commit

Permalink
#6003: include snapshot name in error message (#6015)
Browse files Browse the repository at this point in the history
* #6003: include snapshot name in error message

* #6003: added changelog message

* #6003: wrapped snapshot name and count in double quotes

* #6003: wrapped snapshot name and count in double quotes -> updated snap

* #6003: fixed lint issues in changelog

* #6003: writing complete snapshot key to output
  • Loading branch information
Hannes Römer authored and cpojer committed Apr 18, 2018
1 parent 43e67da commit fb356ac
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
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` +
(diffMessage ||
EXPECTED_COLOR('- ' + (expected || '')) +
'\n' +
Expand Down

0 comments on commit fb356ac

Please sign in to comment.