diff --git a/CHANGELOG.md b/CHANGELOG.md index e061d9be64c7..2c5c599deec7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[jest-jasmine2]` Stop adding `:` after an error that has no message ([#9990](https://github.com/facebook/jest/pull/9990)) - `[jest-diff]` Control no diff message color with `commonColor` in diff options ([#9997](https://github.com/facebook/jest/pull/9997)) - `[jest-snapshot]` Fix TypeScript compilation ([#10008](https://github.com/facebook/jest/pull/10008)) +- `[expect]` Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119)) ### Chore & Maintenance diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index aa0d250d4cb4..3fded8e2ef74 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1958,6 +1958,37 @@ exports[`.toContain(), .toContainEqual() error cases 1`] = ` Received has value: null `; +exports[`.toContain(), .toContainEqual() error cases 2`] = ` +expect(-0).toContain(0) // indexOf + +Matcher error: expected value must be a string if received value is a string + +Expected has type: number +Expected has value: -0 +Received has type: string +Received has value: "-0" +`; + +exports[`.toContain(), .toContainEqual() error cases 3`] = ` +expect(null).toContain(null) // indexOf + +Matcher error: expected value must be a string if received value is a string + +Expected has value: null +Received has type: string +Received has value: "null" +`; + +exports[`.toContain(), .toContainEqual() error cases 4`] = ` +expect(undefined).toContain(undefined) // indexOf + +Matcher error: expected value must be a string if received value is a string + +Expected has value: undefined +Received has type: string +Received has value: "undefined" +`; + 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 4223f4e53475..fd70871423e8 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1463,6 +1463,13 @@ describe('.toContain(), .toContainEqual()', () => { test('error cases', () => { expect(() => jestExpect(null).toContain(1)).toThrowErrorMatchingSnapshot(); + expect(() => jestExpect('-0').toContain(-0)).toThrowErrorMatchingSnapshot(); + expect(() => + jestExpect('null').toContain(null), + ).toThrowErrorMatchingSnapshot(); + expect(() => + jestExpect('undefined').toContain(undefined), + ).toThrowErrorMatchingSnapshot(); }); [ diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index de234b24b0b3..6ebceabe14d9 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -472,6 +472,35 @@ const matchers: MatchersObject = { } if (typeof received === 'string') { + const wrongTypeErrorMessage = `${EXPECTED_COLOR( + 'expected', + )} value must be a string if ${RECEIVED_COLOR( + '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') { + throw new Error( + matcherErrorMessage( + matcherHint(matcherName, received, String(expected), options), + wrongTypeErrorMessage, + printWithType('Expected', expected, printExpected) + + '\n' + + printWithType('Received', received, printReceived), + ), + ); + } + const index = received.indexOf(String(expected)); const pass = index !== -1;