From b02325ae2f70cfef1615431a9c78d0a7839c3091 Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Tue, 15 Aug 2017 18:55:20 -0700 Subject: [PATCH] Make toBe matcher error message more helpful for objects and arrays --- .../__snapshots__/matchers.test.js.snap | 27 +++---------------- packages/jest-matchers/src/matchers.js | 12 +++++++++ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap index 238a0944159f..ce84c22a733e 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap @@ -232,27 +232,13 @@ Received: exports[`.toBe() fails for: [] and [] 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): - [] -Received: - [] - -Difference: - -Compared values have no visual difference." +Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): - {\\"a\\": 1} -Received: - {\\"a\\": 1} - -Difference: - -Compared values have no visual difference." +Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` @@ -277,14 +263,7 @@ Difference: exports[`.toBe() fails for: {} and {} 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): - {} -Received: - {} - -Difference: - -Compared values have no visual difference." +Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead." `; exports[`.toBe() fails for: 1 and 2 1`] = ` diff --git a/packages/jest-matchers/src/matchers.js b/packages/jest-matchers/src/matchers.js index 443671603946..9ae4f5b6c3d7 100644 --- a/packages/jest-matchers/src/matchers.js +++ b/packages/jest-matchers/src/matchers.js @@ -67,6 +67,18 @@ const matchers: MatchersObject = { `Received:\n` + ` ${printReceived(received)}` : () => { + if ( + getType(received) === getType(expected) && + (getType(received) === 'object' || getType(expected) === 'array') && + equals(received, expected, [iterableEquality]) + ) { + return ( + matcherHint('.toBe') + + '\n\n' + + 'Looks like you wanted to test for object/array equity with strict `toBe` matcher. You probably need to use `toEqual` instead.' + ); + } + const diffString = diff(expected, received, { expand: this.expand, });