From f647a6e9ddc4183f064fbb52da5ed5c8c1fbbba8 Mon Sep 17 00:00:00 2001 From: Dave Houlbrooke Date: Wed, 29 Aug 2018 15:37:27 +0100 Subject: [PATCH 1/4] matchers.js: toBeInstanceOf() shouldn't error on null/undefined If using toBeInstanceOf() on an undefined or null value Jest attempts to read `undefined.constructor` which generates an unhelpful error along the lines of "TypeError: Cannot read property 'constructor' of undefined". This fixes the bug so that it's more robust and returns the normal matcher error like "Expected X, received X" --- .../__snapshots__/matchers.test.js.snap | 16 ++++++++++++++++ packages/expect/src/__tests__/matchers.test.js | 2 ++ packages/expect/src/matchers.js | 4 +++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index ea01adb411dc..21d7640cca82 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1078,6 +1078,14 @@ Received constructor: Number Received value: 1" `; +exports[`.toBeInstanceOf() failing null and [Function String] 1`] = ` +"expect(value).toBeInstanceOf(constructor) + +Expected constructor: String +Received constructor: +Received value: null" +`; + exports[`.toBeInstanceOf() failing true and [Function Boolean] 1`] = ` "expect(value).toBeInstanceOf(constructor) @@ -1086,6 +1094,14 @@ Received constructor: Boolean Received value: true" `; +exports[`.toBeInstanceOf() failing undefined and [Function String] 1`] = ` +"expect(value).toBeInstanceOf(constructor) + +Expected constructor: String +Received constructor: +Received value: undefined" +`; + exports[`.toBeInstanceOf() passing [] and [Function Array] 1`] = ` "expect(value).not.toBeInstanceOf(constructor) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 9c331c36e07e..36132f537bb1 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -500,6 +500,8 @@ describe('.toBeInstanceOf()', () => { [true, Boolean], [new A(), B], [Object.create(null), A], + [undefined, String], + [null, String], ].forEach(([a, b]) => { test(`failing ${stringify(a)} and ${stringify(b)}`, () => { expect(() => diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 848e8f1de30f..b2282e579531 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -183,7 +183,9 @@ const matchers: MatchersObject = { constructor.name || String(constructor), )}\n` + `Received constructor: ${RECEIVED_COLOR( - received.constructor && received.constructor.name, + received !== undefined && received !== null + ? received.constructor && received.constructor.name + : '', )}\n` + `Received value: ${printReceived(received)}`; From e26db2d6f92e08c8eb7bfe5c093354acc65920a0 Mon Sep 17 00:00:00 2001 From: Dave Houlbrooke Date: Thu, 30 Aug 2018 12:56:00 +0100 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2271ce48f605..db8c1abde6d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixes +- `[expect]` Fix TypeError in `toBeInstanceOf` on `null` or `undefined` ([#6912](https://github.com/facebook/jest/pull/6912)) - `[jest-jasmine2]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917)) - `[jest-circus]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917)) - `[expect]` Fix variadic custom asymmetric matchers ([#6898](https://github.com/facebook/jest/pull/6898)) From 0903f179e96f20017c838c5b13616390025aafb8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 30 Aug 2018 15:03:48 +0200 Subject: [PATCH 3/4] Update matchers.js --- packages/expect/src/matchers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index b2282e579531..0d8d235ab1b4 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -183,7 +183,7 @@ const matchers: MatchersObject = { constructor.name || String(constructor), )}\n` + `Received constructor: ${RECEIVED_COLOR( - received !== undefined && received !== null + received != null ? received.constructor && received.constructor.name : '', )}\n` + From 96c4f699cdfb9b12f277ba57d1f802b46bedd3a7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 30 Aug 2018 15:04:46 +0200 Subject: [PATCH 4/4] rebase, blah --- .../expect/src/__tests__/__snapshots__/matchers.test.js.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 21d7640cca82..400e8b6e424d 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1082,7 +1082,7 @@ exports[`.toBeInstanceOf() failing null and [Function String] 1`] = ` "expect(value).toBeInstanceOf(constructor) Expected constructor: String -Received constructor: +Received constructor: Received value: null" `; @@ -1098,7 +1098,7 @@ exports[`.toBeInstanceOf() failing undefined and [Function String] 1`] = ` "expect(value).toBeInstanceOf(constructor) Expected constructor: String -Received constructor: +Received constructor: Received value: undefined" `;