From 59274dd84fb3a1643687b6e4b007ddaf51b11e39 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Tue, 8 Dec 2020 09:10:54 +0100 Subject: [PATCH 1/2] Ensure toContain only accepts strings when received is a string Follow-up of #10119 --- .../__tests__/__snapshots__/matchers.test.js.snap | 11 +++++++++++ packages/expect/src/__tests__/matchers.test.js | 8 ++++++++ packages/expect/src/matchers.ts | 13 +------------ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 4ad971a5dbf3..ca39274cdb18 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1989,6 +1989,17 @@ Received has type: string Received has value: "undefined" `; +exports[`.toContain(), .toContainEqual() error cases 5`] = ` +expect(false).toContain(false) // indexOf + +Matcher error: expected value must be a string if received value is a string + +Expected has type: boolean +Expected has value: false +Received has type: string +Received has value: "false" +`; + exports[`.toContain(), .toContainEqual() error cases for toContainEqual 1`] = ` expect(received).toContainEqual(expected) // deep equality diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 5f07e34123be..5b24688e69d0 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1470,6 +1470,14 @@ describe('.toContain(), .toContainEqual()', () => { expect(() => jestExpect('undefined').toContain(undefined), ).toThrowErrorMatchingSnapshot(); + expect(() => + jestExpect('false').toContain(false), + ).toThrowErrorMatchingSnapshot(); + if (isBigIntDefined) { + expect(() => jestExpect('1').toContain(BigInt(1))).toThrowError( + 'toContain', + ); + } }); [ diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index fc0074199486..99a3c7e9fd42 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -480,18 +480,7 @@ const matchers: MatchersObject = { 'received', )} value is a string`; - if (expected == null) { - throw new Error( - matcherErrorMessage( - matcherHint(matcherName, received, String(expected), options), - wrongTypeErrorMessage, - printWithType('Expected', expected, printExpected) + - '\n' + - printWithType('Received', received, printReceived), - ), - ); - } - if (typeof expected === 'number') { + if (typeof expected !== 'string') { throw new Error( matcherErrorMessage( matcherHint(matcherName, received, String(expected), options), From 61febc5048959bd75ee0e90214f264c53a4a3ca1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 8 Dec 2020 10:00:06 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 575b81f699b2..f7e8e3b07be7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903)) - `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708)) -- `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119)) +- `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929)) - `[jest-circus]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block [#10451](https://github.com/facebook/jest/issues/10451) - `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871)) - `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))