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),