From 395e93ec121a7a6e752a2e74e257ca507a684d1a Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Tue, 8 Dec 2020 10:00:41 +0100 Subject: [PATCH] Ensure `toContain` only accepts strings when `received` is a string (#10929) --- CHANGELOG.md | 2 +- .../__tests__/__snapshots__/matchers.test.js.snap | 11 +++++++++++ packages/expect/src/__tests__/matchers.test.js | 8 ++++++++ packages/expect/src/matchers.ts | 13 +------------ 4 files changed, 21 insertions(+), 13 deletions(-) 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)) 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),