From 8e6578acf8ba8b435bbd344d08c61af177952b39 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 6 Apr 2019 16:03:56 -0400 Subject: [PATCH 1/8] expect: Improve report when matcher fail, part 15 --- .../__snapshots__/matchers.test.js.snap | 153 +++++++++++------- .../expect/src/__tests__/matchers.test.js | 7 + packages/expect/src/matchers.ts | 68 +++++--- packages/jest-matcher-utils/src/index.ts | 6 +- 4 files changed, 150 insertions(+), 84 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index b1234a0c609b..ff399fe93fa5 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -225,12 +225,7 @@ Received has value: undefined" `; exports[`.toBe() does not crash on circular references 1`] = ` -"expect(received).toBe(expected) // Object.is equality - -Expected: {} -Received: {\\"circular\\": [Circular]} - -Difference: +"expect(received).toBe(expected) // Object.is equality - Expected + Received @@ -242,99 +237,131 @@ Difference: `; exports[`.toBe() fails for '"a"' with '.not' 1`] = ` -"expect(received).not.toBe(expected) // Object.is equality +"expect(received).not.toBe(expected) // Object.is equality -Expected: \\"a\\" -Received: \\"a\\"" +Expected: not \\"a\\"" `; exports[`.toBe() fails for '[]' with '.not' 1`] = ` -"expect(received).not.toBe(expected) // Object.is equality +"expect(received).not.toBe(expected) // Object.is equality -Expected: [] -Received: []" +Expected: not []" `; exports[`.toBe() fails for '{}' with '.not' 1`] = ` -"expect(received).not.toBe(expected) // Object.is equality +"expect(received).not.toBe(expected) // Object.is equality -Expected: {} -Received: {}" +Expected: not {}" `; exports[`.toBe() fails for '1' with '.not' 1`] = ` -"expect(received).not.toBe(expected) // Object.is equality +"expect(received).not.toBe(expected) // Object.is equality -Expected: 1 -Received: 1" +Expected: not 1" `; exports[`.toBe() fails for 'false' with '.not' 1`] = ` -"expect(received).not.toBe(expected) // Object.is equality +"expect(received).not.toBe(expected) // Object.is equality -Expected: false -Received: false" +Expected: not false" `; exports[`.toBe() fails for 'null' with '.not' 1`] = ` -"expect(received).not.toBe(expected) // Object.is equality +"expect(received).not.toBe(expected) // Object.is equality -Expected: null -Received: null" +Expected: not null" `; exports[`.toBe() fails for 'undefined' with '.not' 1`] = ` -"expect(received).not.toBe(expected) // Object.is equality +"expect(received).not.toBe(expected) // Object.is equality -Expected: undefined -Received: undefined" +Expected: not undefined" `; exports[`.toBe() fails for: "abc" and "cde" 1`] = ` -"expect(received).toBe(expected) // Object.is equality +"expect(received).toBe(expected) // Object.is equality Expected: \\"cde\\" Received: \\"abc\\"" `; +exports[`.toBe() fails for: "four +4 +line +string" and "3 +line +string" 1`] = ` +"expect(received).toBe(expected) // Object.is equality + +- Expected ++ Received + +- 3 ++ four ++ 4 + line + string" +`; + exports[`.toBe() fails for: "with trailing space" and "without trailing space" 1`] = ` -"expect(received).toBe(expected) // Object.is equality +"expect(received).toBe(expected) // Object.is equality Expected: \\"without trailing space\\" Received: \\"with trailing space\\"" `; +exports[`.toBe() fails for: /received/ and /expected/ 1`] = ` +"expect(received).toBe(expected) // Object.is equality + +Expected: /expected/ +Received: /received/" +`; + exports[`.toBe() fails for: [] and [] 1`] = ` -"expect(received).toBe(expected) // Object.is equality +"expect(received).toBe(expected) // Object.is equality Expected: [] -Received: [] +Received value has no visual difference -Difference: +If the test should pass with deep equality, replace toBe with toStrictEqual" +`; -Compared values have no visual difference. Note that you are testing for equality with the stricter \`toBe\` matcher using \`Object.is\`. For deep equality only, use \`toEqual\` instead." +exports[`.toBe() fails for: [Error: received] and [Error: expected] 1`] = ` +"expect(received).toBe(expected) // Object.is equality + +Expected: [Error: expected] +Received: [Error: received]" `; -exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` -"expect(received).toBe(expected) // Object.is equality +exports[`.toBe() fails for: [Function anonymous] and [Function anonymous] 1`] = ` +"expect(received).toBe(expected) // Object.is equality -Expected: {\\"a\\": 1} -Received: {\\"a\\": 1} +Expected: [Function anonymous] +Received value has no visual difference" +`; -Difference: +exports[`.toBe() fails for: {"a": [Function a], "b": 2} and {"a": Any, "b": 2} 1`] = ` +"expect(received).toBe(expected) // Object.is equality + +Expected: {\\"a\\": Any, \\"b\\": 2} +Received: {\\"a\\": [Function a], \\"b\\": 2} -Compared values have no visual difference. Note that you are testing for equality with the stricter \`toBe\` matcher using \`Object.is\`. For deep equality only, use \`toEqual\` instead." +If the test should pass with deep equality, replace toBe with toStrictEqual" `; -exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` -"expect(received).toBe(expected) // Object.is equality +exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` +"expect(received).toBe(expected) // Object.is equality -Expected: {\\"a\\": 5} -Received: {\\"a\\": 1} +Expected: {\\"a\\": 1} +Received value has no visual difference -Difference: +If the test should pass with deep equality, replace toBe with toStrictEqual" +`; + +exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` +"expect(received).toBe(expected) // Object.is equality - Expected + Received @@ -345,44 +372,54 @@ Difference: }" `; +exports[`.toBe() fails for: {"a": undefined, "b": 2} and {"b": 2} 1`] = ` +"expect(received).toBe(expected) // Object.is equality + +Expected: {\\"b\\": 2} +Received: {\\"a\\": undefined, \\"b\\": 2} + +If the test should pass with deep equality, replace toBe with toEqual" +`; + exports[`.toBe() fails for: {} and {} 1`] = ` -"expect(received).toBe(expected) // Object.is equality +"expect(received).toBe(expected) // Object.is equality Expected: {} -Received: {} +Received value has no visual difference -Difference: - -Compared values have no visual difference. Note that you are testing for equality with the stricter \`toBe\` matcher using \`Object.is\`. For deep equality only, use \`toEqual\` instead." +If the test should pass with deep equality, replace toBe with toStrictEqual" `; exports[`.toBe() fails for: -0 and 0 1`] = ` -"expect(received).toBe(expected) // Object.is equality +"expect(received).toBe(expected) // Object.is equality Expected: 0 Received: -0" `; exports[`.toBe() fails for: 1 and 2 1`] = ` -"expect(received).toBe(expected) // Object.is equality +"expect(received).toBe(expected) // Object.is equality Expected: 2 Received: 1" `; -exports[`.toBe() fails for: null and undefined 1`] = ` -"expect(received).toBe(expected) // Object.is equality +exports[`.toBe() fails for: Symbol(received) and Symbol(expected) 1`] = ` +"expect(received).toBe(expected) // Object.is equality -Expected: undefined -Received: null +Expected: Symbol(expected) +Received: Symbol(received)" +`; -Difference: +exports[`.toBe() fails for: null and undefined 1`] = ` +"expect(received).toBe(expected) // Object.is equality - Comparing two different types of values. Expected undefined but received null." +Expected: undefined +Received: null" `; exports[`.toBe() fails for: true and false 1`] = ` -"expect(received).toBe(expected) // Object.is equality +"expect(received).toBe(expected) // Object.is equality Expected: false Received: true" diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 953de2f2fcaf..ff365c464e2a 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -203,11 +203,18 @@ describe('.toBe()', () => { [ [1, 2], [true, false], + [() => {}, () => {}], [{}, {}], [{a: 1}, {a: 1}], [{a: 1}, {a: 5}], + [{a: () => {}, b: 2}, {a: expect.any(Function), b: 2}], + [{a: undefined, b: 2}, {b: 2}], + [/received/, /expected/], + [Symbol('received'), Symbol('expected')], + [new Error('received'), new Error('expected')], ['abc', 'cde'], ['with \ntrailing space', 'without trailing space'], + ['four\n4\nline\nstring', '3\nline\nstring'], [[], []], [null, undefined], [-0, +0], diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index 6b94eaf11d81..2e85ed4da306 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -8,9 +8,9 @@ import getType, {isPrimitive} from 'jest-get-type'; import { + DIM_COLOR, EXPECTED_COLOR, RECEIVED_COLOR, - SUGGEST_TO_EQUAL, SUGGEST_TO_CONTAIN_EQUAL, diff, ensureExpectedIsNonNegativeInteger, @@ -22,6 +22,7 @@ import { printReceived, printExpected, printWithType, + stringify, MatcherHintOptions, } from 'jest-matcher-utils'; import {MatchersObject, MatcherState} from './types'; @@ -41,6 +42,12 @@ import { } from './utils'; import {equals} from './jasmineUtils'; +const toStrictEqualMatchers = [ + iterableEquality, + typeEquality, + sparseArrayEquality, +]; + type ContainIterable = | Array | Set @@ -50,10 +57,11 @@ type ContainIterable = const matchers: MatchersObject = { toBe(this: MatcherState, received: unknown, expected: unknown) { - const matcherName = '.toBe'; + const matcherName = 'toBe'; const options: MatcherHintOptions = { comment: 'Object.is equality', isNot: this.isNot, + promise: this.promise, }; const pass = Object.is(received, expected); @@ -62,32 +70,55 @@ const matchers: MatchersObject = { ? () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - `Expected: ${printExpected(expected)}\n` + - `Received: ${printReceived(received)}` + `Expected: not ${printExpected(expected)}` : () => { const receivedType = getType(received); const expectedType = getType(expected); - const suggestToEqual = - receivedType === expectedType && - (receivedType === 'object' || expectedType === 'array') && - equals(received, expected, [iterableEquality]); - const oneline = isOneline(expected, received); - const diffString = diff(expected, received, {expand: this.expand}); + + const isShallow = + receivedType !== expectedType || + (isPrimitive(expected) && + (expectedType !== 'string' || isOneline(expected, received))) || + expectedType === 'function' || + expectedType === 'regexp' || + (received instanceof Error && expected instanceof Error); + + const deepEqualityName = + isShallow || (expectedType !== 'object' && expectedType !== 'array') + ? '' + : equals(received, expected, toStrictEqualMatchers, true) + ? 'toStrictEqual' + : equals(received, expected, [iterableEquality]) + ? 'toEqual' + : ''; + + const hasDifference = stringify(expected) !== stringify(received); + const difference = + !isShallow && deepEqualityName === '' && hasDifference + ? diff(expected, received, {expand: this.expand}) + : null; return ( matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - `Expected: ${printExpected(expected)}\n` + - `Received: ${printReceived(received)}` + - (diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '') + - (suggestToEqual ? ` ${SUGGEST_TO_EQUAL}` : '') + (difference + ? difference + : `Expected: ${printExpected(expected)}\n` + + (hasDifference + ? `Received: ${printReceived(received)}` + : 'Received value has no visual difference') + + (deepEqualityName && + '\n\n' + + DIM_COLOR( + `If the test should pass with deep equality, replace toBe with ${deepEqualityName}`, + ))) ); }; // Passing the actual and expected objects so that a custom reporter // could access them, for example in order to display a custom visual diff, // or create a different error message - return {actual: received, expected, message, name: 'toBe', pass}; + return {actual: received, expected, message, name: matcherName, pass}; }, toBeCloseTo( @@ -856,12 +887,7 @@ const matchers: MatchersObject = { isNot: this.isNot, }; - const pass = equals( - received, - expected, - [iterableEquality, typeEquality, sparseArrayEquality], - true, - ); + const pass = equals(received, expected, toStrictEqualMatchers, true); const message = pass ? () => diff --git a/packages/jest-matcher-utils/src/index.ts b/packages/jest-matcher-utils/src/index.ts index 89fd60568e6b..9f9de13390ce 100644 --- a/packages/jest-matcher-utils/src/index.ts +++ b/packages/jest-matcher-utils/src/index.ts @@ -41,7 +41,7 @@ export const EXPECTED_COLOR = chalk.green; export const RECEIVED_COLOR = chalk.red; export const INVERTED_COLOR = chalk.inverse; export const BOLD_WEIGHT = chalk.bold; -const DIM_COLOR = chalk.dim; +export const DIM_COLOR = chalk.dim; const NUMBERS = [ 'zero', @@ -60,10 +60,6 @@ const NUMBERS = [ 'thirteen', ]; -export const SUGGEST_TO_EQUAL = chalk.dim( - 'Note that you are testing for equality with the stricter `toBe` matcher using `Object.is`. For deep equality only, use `toEqual` instead.', -); - export const SUGGEST_TO_CONTAIN_EQUAL = chalk.dim( 'Looks like you wanted to test for object/array equality with the stricter `toContain` matcher. You probably need to use `toContainEqual` instead.', ); From 09300a631e627ecdc58afa59e64e7b3c05892f85 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 6 Apr 2019 16:14:55 -0400 Subject: [PATCH 2/8] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 549d7efa84ec..33b50836ad7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[expect]` Improve report when matcher fails, part 15 ([#8281](https://github.com/facebook/jest/pull/8281)) + ### Fixes - `[jest-snapshot]` Inline snapshots: do not indent empty lines ([#8277](https://github.com/facebook/jest/pull/8277)) From 6aa3cd01d25b067ae558c37356163c99e8591da7 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 7 Apr 2019 11:15:02 -0400 Subject: [PATCH 3/8] Rename isShallowInequality, refactor deepEqualityName, edit and move message --- .../__snapshots__/matchers.test.js.snap | 41 ++++++++++++------- packages/expect/src/matchers.ts | 39 ++++++++++-------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index ff399fe93fa5..45a7726c605f 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -322,10 +322,10 @@ Received: /received/" exports[`.toBe() fails for: [] and [] 1`] = ` "expect(received).toBe(expected) // Object.is equality -Expected: [] -Received value has no visual difference +If it should pass with deep equality, replace toBe with toStrictEqual -If the test should pass with deep equality, replace toBe with toStrictEqual" +Expected: [] +Received value has no visual difference" `; exports[`.toBe() fails for: [Error: received] and [Error: expected] 1`] = ` @@ -345,19 +345,25 @@ Received value has no visual difference" exports[`.toBe() fails for: {"a": [Function a], "b": 2} and {"a": Any, "b": 2} 1`] = ` "expect(received).toBe(expected) // Object.is equality -Expected: {\\"a\\": Any, \\"b\\": 2} -Received: {\\"a\\": [Function a], \\"b\\": 2} +If it should pass with deep equality, replace toBe with toStrictEqual + +- Expected ++ Received -If the test should pass with deep equality, replace toBe with toStrictEqual" + Object { +- \\"a\\": Any, ++ \\"a\\": [Function a], + \\"b\\": 2, + }" `; exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` "expect(received).toBe(expected) // Object.is equality -Expected: {\\"a\\": 1} -Received value has no visual difference +If it should pass with deep equality, replace toBe with toStrictEqual -If the test should pass with deep equality, replace toBe with toStrictEqual" +Expected: {\\"a\\": 1} +Received value has no visual difference" `; exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` @@ -375,19 +381,24 @@ exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` exports[`.toBe() fails for: {"a": undefined, "b": 2} and {"b": 2} 1`] = ` "expect(received).toBe(expected) // Object.is equality -Expected: {\\"b\\": 2} -Received: {\\"a\\": undefined, \\"b\\": 2} +If it should pass with deep equality, replace toBe with toEqual -If the test should pass with deep equality, replace toBe with toEqual" +- Expected ++ Received + + Object { ++ \\"a\\": undefined, + \\"b\\": 2, + }" `; exports[`.toBe() fails for: {} and {} 1`] = ` "expect(received).toBe(expected) // Object.is equality -Expected: {} -Received value has no visual difference +If it should pass with deep equality, replace toBe with toStrictEqual -If the test should pass with deep equality, replace toBe with toStrictEqual" +Expected: {} +Received value has no visual difference" `; exports[`.toBe() fails for: -0 and 0 1`] = ` diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index 2e85ed4da306..eac3e960faa4 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -75,7 +75,7 @@ const matchers: MatchersObject = { const receivedType = getType(received); const expectedType = getType(expected); - const isShallow = + const isShallowInequality = receivedType !== expectedType || (isPrimitive(expected) && (expectedType !== 'string' || isOneline(expected, received))) || @@ -83,35 +83,38 @@ const matchers: MatchersObject = { expectedType === 'regexp' || (received instanceof Error && expected instanceof Error); - const deepEqualityName = - isShallow || (expectedType !== 'object' && expectedType !== 'array') - ? '' - : equals(received, expected, toStrictEqualMatchers, true) - ? 'toStrictEqual' - : equals(received, expected, [iterableEquality]) - ? 'toEqual' - : ''; + let deepEqualityName = null; + if (!isShallowInequality) { + if (expectedType === 'object' || expectedType === 'array') { + // If deep equality could pass when referential identity fails + if (equals(received, expected, toStrictEqualMatchers, true)) { + deepEqualityName = 'toStrictEqual'; + } else if (equals(received, expected, [iterableEquality])) { + deepEqualityName = 'toEqual'; + } + } + } const hasDifference = stringify(expected) !== stringify(received); const difference = - !isShallow && deepEqualityName === '' && hasDifference - ? diff(expected, received, {expand: this.expand}) + hasDifference && !isShallowInequality + ? diff(expected, received, {expand: this.expand}) // string | null : null; return ( matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - (difference + (deepEqualityName !== null + ? DIM_COLOR( + `If it should pass with deep equality, replace toBe with ${deepEqualityName}`, + ) + '\n\n' + : '') + + (difference !== null ? difference : `Expected: ${printExpected(expected)}\n` + (hasDifference ? `Received: ${printReceived(received)}` - : 'Received value has no visual difference') + - (deepEqualityName && - '\n\n' + - DIM_COLOR( - `If the test should pass with deep equality, replace toBe with ${deepEqualityName}`, - ))) + : 'Received value has no visual difference')) ); }; From c98a34943889d97c1c1e3953f357a861ccda26e1 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 7 Apr 2019 14:29:04 -0400 Subject: [PATCH 4/8] Rename toStrictEqualTesters --- packages/expect/src/matchers.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index eac3e960faa4..db8cfec100ba 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -42,7 +42,7 @@ import { } from './utils'; import {equals} from './jasmineUtils'; -const toStrictEqualMatchers = [ +const toStrictEqualTesters = [ iterableEquality, typeEquality, sparseArrayEquality, @@ -87,7 +87,7 @@ const matchers: MatchersObject = { if (!isShallowInequality) { if (expectedType === 'object' || expectedType === 'array') { // If deep equality could pass when referential identity fails - if (equals(received, expected, toStrictEqualMatchers, true)) { + if (equals(received, expected, toStrictEqualTesters, true)) { deepEqualityName = 'toStrictEqual'; } else if (equals(received, expected, [iterableEquality])) { deepEqualityName = 'toEqual'; @@ -890,7 +890,7 @@ const matchers: MatchersObject = { isNot: this.isNot, }; - const pass = equals(received, expected, toStrictEqualMatchers, true); + const pass = equals(received, expected, toStrictEqualTesters, true); const message = pass ? () => From 685312c8e4ca4c314406843c6b282dfedc66e7e3 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 7 Apr 2019 15:01:53 -0400 Subject: [PATCH 5/8] Separate deepEqualityName from hasConciseReport --- packages/expect/src/matchers.ts | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index db8cfec100ba..0f3b0d99bb1b 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -75,29 +75,28 @@ const matchers: MatchersObject = { const receivedType = getType(received); const expectedType = getType(expected); - const isShallowInequality = + let deepEqualityName = null; + if (expectedType !== 'map' && expectedType !== 'set') { + // If deep equality passes when referential identity fails, + // but exclude map and set until review of their equality logic. + if (equals(received, expected, toStrictEqualTesters, true)) { + deepEqualityName = 'toStrictEqual'; + } else if (equals(received, expected, [iterableEquality])) { + deepEqualityName = 'toEqual'; + } + } + + const hasConciseReport = receivedType !== expectedType || (isPrimitive(expected) && (expectedType !== 'string' || isOneline(expected, received))) || + expectedType === 'date' || expectedType === 'function' || expectedType === 'regexp' || (received instanceof Error && expected instanceof Error); - - let deepEqualityName = null; - if (!isShallowInequality) { - if (expectedType === 'object' || expectedType === 'array') { - // If deep equality could pass when referential identity fails - if (equals(received, expected, toStrictEqualTesters, true)) { - deepEqualityName = 'toStrictEqual'; - } else if (equals(received, expected, [iterableEquality])) { - deepEqualityName = 'toEqual'; - } - } - } - const hasDifference = stringify(expected) !== stringify(received); const difference = - hasDifference && !isShallowInequality + !hasConciseReport && hasDifference ? diff(expected, received, {expand: this.expand}) // string | null : null; From a019b33e1777b5781a13e462b789bc2b233e6dca Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 7 Apr 2019 15:02:13 -0400 Subject: [PATCH 6/8] Add 2 snapshot tests for date --- .../__snapshots__/matchers.test.js.snap | 16 ++++++++++++++++ packages/expect/src/__tests__/matchers.test.js | 2 ++ 2 files changed, 18 insertions(+) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 45a7726c605f..8a857690271d 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -415,6 +415,22 @@ Expected: 2 Received: 1" `; +exports[`.toBe() fails for: 2020-02-20T00:00:00.000Z and 2020-02-20T00:00:00.000Z 1`] = ` +"expect(received).toBe(expected) // Object.is equality + +If it should pass with deep equality, replace toBe with toStrictEqual + +Expected: 2020-02-20T00:00:00.000Z +Received value has no visual difference" +`; + +exports[`.toBe() fails for: 2020-02-21T00:00:00.000Z and 2020-02-20T00:00:00.000Z 1`] = ` +"expect(received).toBe(expected) // Object.is equality + +Expected: 2020-02-20T00:00:00.000Z +Received: 2020-02-21T00:00:00.000Z" +`; + exports[`.toBe() fails for: Symbol(received) and Symbol(expected) 1`] = ` "expect(received).toBe(expected) // Object.is equality diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index ff365c464e2a..9331fb30e6cb 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -209,6 +209,8 @@ describe('.toBe()', () => { [{a: 1}, {a: 5}], [{a: () => {}, b: 2}, {a: expect.any(Function), b: 2}], [{a: undefined, b: 2}, {b: 2}], + [new Date('2020-02-20'), new Date('2020-02-20')], + [new Date('2020-02-21'), new Date('2020-02-20')], [/received/, /expected/], [Symbol('received'), Symbol('expected')], [new Error('received'), new Error('expected')], From e19dc1a089d6e331d0fb46bc205d835c7951f45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 8 Apr 2019 09:11:00 -0400 Subject: [PATCH 7/8] Update packages/expect/src/matchers.ts Co-Authored-By: pedrottimark --- packages/expect/src/matchers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index 0f3b0d99bb1b..405b479330cc 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -105,7 +105,7 @@ const matchers: MatchersObject = { '\n\n' + (deepEqualityName !== null ? DIM_COLOR( - `If it should pass with deep equality, replace toBe with ${deepEqualityName}`, + `If it should pass with deep equality, replace "${matcherName}" with "${deepEqualityName}"`, ) + '\n\n' : '') + (difference !== null From 93b60490993493ebee5466b9ad3049444d56a662 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 8 Apr 2019 09:25:04 -0400 Subject: [PATCH 8/8] Update 6 snapshots for improved deep equality message --- .../__tests__/__snapshots__/matchers.test.js.snap | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 8a857690271d..d565aa057f99 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -322,7 +322,7 @@ Received: /received/" exports[`.toBe() fails for: [] and [] 1`] = ` "expect(received).toBe(expected) // Object.is equality -If it should pass with deep equality, replace toBe with toStrictEqual +If it should pass with deep equality, replace \\"toBe\\" with \\"toStrictEqual\\" Expected: [] Received value has no visual difference" @@ -345,7 +345,7 @@ Received value has no visual difference" exports[`.toBe() fails for: {"a": [Function a], "b": 2} and {"a": Any, "b": 2} 1`] = ` "expect(received).toBe(expected) // Object.is equality -If it should pass with deep equality, replace toBe with toStrictEqual +If it should pass with deep equality, replace \\"toBe\\" with \\"toStrictEqual\\" - Expected + Received @@ -360,7 +360,7 @@ exports[`.toBe() fails for: {"a": [Function a], "b": 2} and {"a": Any, exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` "expect(received).toBe(expected) // Object.is equality -If it should pass with deep equality, replace toBe with toStrictEqual +If it should pass with deep equality, replace \\"toBe\\" with \\"toStrictEqual\\" Expected: {\\"a\\": 1} Received value has no visual difference" @@ -381,7 +381,7 @@ exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` exports[`.toBe() fails for: {"a": undefined, "b": 2} and {"b": 2} 1`] = ` "expect(received).toBe(expected) // Object.is equality -If it should pass with deep equality, replace toBe with toEqual +If it should pass with deep equality, replace \\"toBe\\" with \\"toEqual\\" - Expected + Received @@ -395,7 +395,7 @@ exports[`.toBe() fails for: {"a": undefined, "b": 2} and {"b": 2} 1`] = ` exports[`.toBe() fails for: {} and {} 1`] = ` "expect(received).toBe(expected) // Object.is equality -If it should pass with deep equality, replace toBe with toStrictEqual +If it should pass with deep equality, replace \\"toBe\\" with \\"toStrictEqual\\" Expected: {} Received value has no visual difference" @@ -418,7 +418,7 @@ Received: 1" exports[`.toBe() fails for: 2020-02-20T00:00:00.000Z and 2020-02-20T00:00:00.000Z 1`] = ` "expect(received).toBe(expected) // Object.is equality -If it should pass with deep equality, replace toBe with toStrictEqual +If it should pass with deep equality, replace \\"toBe\\" with \\"toStrictEqual\\" Expected: 2020-02-20T00:00:00.000Z Received value has no visual difference"