From d3a081cba1a8fff0b449de6ab262de3df7f39425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20R=C3=B6mer?= Date: Tue, 17 Apr 2018 15:56:19 +0200 Subject: [PATCH 1/6] #6003: include snapshot name in error message --- .../__snapshots__/failures.test.js.snap | 26 +++++++++++++++++++ integration-tests/__tests__/failures.test.js | 10 +++++++ .../__snapshots__/snapshot_named.test.js.snap | 3 +++ .../failures/__tests__/snapshot_named.test.js | 13 ++++++++++ packages/jest-snapshot/src/index.js | 4 ++- 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 integration-tests/failures/__tests__/__snapshots__/snapshot_named.test.js.snap create mode 100644 integration-tests/failures/__tests__/snapshot_named.test.js diff --git a/integration-tests/__tests__/__snapshots__/failures.test.js.snap b/integration-tests/__tests__/__snapshots__/failures.test.js.snap index 852eed0e2a00..51fa428d2380 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 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 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/index.js b/packages/jest-snapshot/src/index.js index a76e765e8929..6725b0d588d5 100644 --- a/packages/jest-snapshot/src/index.js +++ b/packages/jest-snapshot/src/index.js @@ -92,7 +92,9 @@ 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 ${testName ? testName + ' ' : ''}${count}`, + )}.\n\n` + (diffMessage || EXPECTED_COLOR('- ' + (expected || '')) + '\n' + From beb8d1ce5f6b3cc60a966fd004cf8283d22aaaa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20R=C3=B6mer?= Date: Tue, 17 Apr 2018 16:26:57 +0200 Subject: [PATCH 2/6] #6003: added changelog message --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d7a5369711d..1ed5022a2ea8 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)) From 8c97d24f47d4d62a2ff7a15324c5df08c16629a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20R=C3=B6mer?= Date: Tue, 17 Apr 2018 16:49:45 +0200 Subject: [PATCH 3/6] #6003: wrapped snapshot name and count in double quotes --- packages/jest-snapshot/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/index.js b/packages/jest-snapshot/src/index.js index 6725b0d588d5..55b4da34ca1f 100644 --- a/packages/jest-snapshot/src/index.js +++ b/packages/jest-snapshot/src/index.js @@ -93,7 +93,7 @@ const toMatchSnapshot = function(received: any, testName?: string) { report = () => `${RECEIVED_COLOR('Received value')} does not match ` + `${EXPECTED_COLOR( - `stored snapshot ${testName ? testName + ' ' : ''}${count}`, + `stored snapshot "${testName ? testName + ' ' : ''}${count}"`, )}.\n\n` + (diffMessage || EXPECTED_COLOR('- ' + (expected || '')) + From a19b4d44b72c0b66e8ec360a74179ac1b157e2cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20R=C3=B6mer?= Date: Tue, 17 Apr 2018 17:01:56 +0200 Subject: [PATCH 4/6] #6003: wrapped snapshot name and count in double quotes -> updated snap --- .../__tests__/__snapshots__/failures.test.js.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration-tests/__tests__/__snapshots__/failures.test.js.snap b/integration-tests/__tests__/__snapshots__/failures.test.js.snap index 51fa428d2380..9ef16a206cab 100644 --- a/integration-tests/__tests__/__snapshots__/failures.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/failures.test.js.snap @@ -407,7 +407,7 @@ exports[`works with named snapshot failures 1`] = ` expect(value).toMatchSnapshot() - Received value does not match stored snapshot snapname 1. + Received value does not match stored snapshot \\"snapname 1\\". - \\"bar\\" + \\"foo\\" @@ -854,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 \\"1\\". - \\"bar\\" + \\"foo\\" From c5414b0b39e2c0398aa2c2da821f600ed5a89847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20R=C3=B6mer?= Date: Tue, 17 Apr 2018 17:05:55 +0200 Subject: [PATCH 5/6] #6003: fixed lint issues in changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ed5022a2ea8..8b117fe5e892 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ### Features -* `[jest-snapshot]` [**BREAKING**] Concatenate name of test, optional snapshot name and count - ([#6015](https://github.com/facebook/jest/pull/6015)) +* `[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)) From 7e470e8897d5e54c07e82c587fa8416cffd1f3e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20R=C3=B6mer?= Date: Tue, 17 Apr 2018 17:46:59 +0200 Subject: [PATCH 6/6] #6003: writing complete snapshot key to output --- .../__tests__/__snapshots__/failures.test.js.snap | 4 ++-- packages/jest-snapshot/src/State.js | 3 +++ packages/jest-snapshot/src/index.js | 6 ++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/integration-tests/__tests__/__snapshots__/failures.test.js.snap b/integration-tests/__tests__/__snapshots__/failures.test.js.snap index 9ef16a206cab..6aee550ffc9b 100644 --- a/integration-tests/__tests__/__snapshots__/failures.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/failures.test.js.snap @@ -407,7 +407,7 @@ exports[`works with named snapshot failures 1`] = ` expect(value).toMatchSnapshot() - Received value does not match stored snapshot \\"snapname 1\\". + Received value does not match stored snapshot \\"failing named snapshot: snapname 1\\". - \\"bar\\" + \\"foo\\" @@ -854,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/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 55b4da34ca1f..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,9 +92,7 @@ const toMatchSnapshot = function(received: any, testName?: string) { report = () => `${RECEIVED_COLOR('Received value')} does not match ` + - `${EXPECTED_COLOR( - `stored snapshot "${testName ? testName + ' ' : ''}${count}"`, - )}.\n\n` + + `${EXPECTED_COLOR(`stored snapshot "${result.key}"`)}.\n\n` + (diffMessage || EXPECTED_COLOR('- ' + (expected || '')) + '\n' +