diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d7a5369711d..8b117fe5e892 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/integration-tests/__tests__/__snapshots__/failures.test.js.snap b/integration-tests/__tests__/__snapshots__/failures.test.js.snap index 852eed0e2a00..6aee550ffc9b 100644 --- a/integration-tests/__tests__/__snapshots__/failures.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/failures.test.js.snap @@ -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 @@ -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\\" diff --git a/integration-tests/__tests__/failures.test.js b/integration-tests/__tests__/failures.test.js index 9e0d689a20ac..97b8b577daf2 100644 --- a/integration-tests/__tests__/failures.test.js +++ b/integration-tests/__tests__/failures.test.js @@ -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(); +}); diff --git a/integration-tests/failures/__tests__/__snapshots__/snapshot_named.test.js.snap b/integration-tests/failures/__tests__/__snapshots__/snapshot_named.test.js.snap new file mode 100644 index 000000000000..593f8e2e92d5 --- /dev/null +++ b/integration-tests/failures/__tests__/__snapshots__/snapshot_named.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`failing named snapshot: snapname 1`] = `"bar"`; diff --git a/integration-tests/failures/__tests__/snapshot_named.test.js b/integration-tests/failures/__tests__/snapshot_named.test.js new file mode 100644 index 000000000000..cddb7abc7f34 --- /dev/null +++ b/integration-tests/failures/__tests__/snapshot_named.test.js @@ -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'); +}); diff --git a/packages/jest-snapshot/src/State.js b/packages/jest-snapshot/src/State.js index 3de407f37190..7f9469da1148 100644 --- a/packages/jest-snapshot/src/State.js +++ b/packages/jest-snapshot/src/State.js @@ -165,6 +165,7 @@ export default class SnapshotState { actual: '', count, expected: '', + key, pass: true, }; } else { @@ -174,6 +175,7 @@ export default class SnapshotState { actual: unescape(receivedSerialized), count, expected: expected ? unescape(expected) : null, + key, pass: false, }; } else { @@ -182,6 +184,7 @@ export default class SnapshotState { actual: '', count, expected: '', + key, pass: true, }; } diff --git a/packages/jest-snapshot/src/index.js b/packages/jest-snapshot/src/index.js index a76e765e8929..5bc81da90d94 100644 --- a/packages/jest-snapshot/src/index.js +++ b/packages/jest-snapshot/src/index.js @@ -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; @@ -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' +