Skip to content

Commit 8609c5c

Browse files
committed
Make toContain more strict with the received type
Fixes #8023
1 parent 81712ba commit 8609c5c

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `[jest-jasmine2]` Stop adding `:` after an error that has no message ([#9990](https://github.com/facebook/jest/pull/9990))
1111
- `[jest-diff]` Control no diff message color with `commonColor` in diff options ([#9997](https://github.com/facebook/jest/pull/9997))
1212
- `[jest-snapshot]` Fix TypeScript compilation ([#10008](https://github.com/facebook/jest/pull/10008))
13+
- `[expect]` Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119))
1314

1415
### Chore & Maintenance
1516

packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,37 @@ exports[`.toContain(), .toContainEqual() error cases 1`] = `
19581958
Received has value: <r>null</>
19591959
`;
19601960

1961+
exports[`.toContain(), .toContainEqual() error cases 2`] = `
1962+
<d>expect(</><r>-0</><d>).</>toContain<d>(</><g>0</><d>) // indexOf</>
1963+
1964+
<b>Matcher error</>: <g>expected</> value must be a string if <r>received</> value is a string
1965+
1966+
Expected has type: number
1967+
Expected has value: <g>-0</>
1968+
Received has type: string
1969+
Received has value: <r>"-0"</>
1970+
`;
1971+
1972+
exports[`.toContain(), .toContainEqual() error cases 3`] = `
1973+
<d>expect(</><r>null</><d>).</>toContain<d>(</><g>null</><d>) // indexOf</>
1974+
1975+
<b>Matcher error</>: <g>expected</> value must be a string if <r>received</> value is a string
1976+
1977+
Expected has value: <g>null</>
1978+
Received has type: string
1979+
Received has value: <r>"null"</>
1980+
`;
1981+
1982+
exports[`.toContain(), .toContainEqual() error cases 4`] = `
1983+
<d>expect(</><r>undefined</><d>).</>toContain<d>(</><g>undefined</><d>) // indexOf</>
1984+
1985+
<b>Matcher error</>: <g>expected</> value must be a string if <r>received</> value is a string
1986+
1987+
Expected has value: <g>undefined</>
1988+
Received has type: string
1989+
Received has value: <r>"undefined"</>
1990+
`;
1991+
19611992
exports[`.toContain(), .toContainEqual() error cases for toContainEqual 1`] = `
19621993
<d>expect(</><r>received</><d>).</>toContainEqual<d>(</><g>expected</><d>) // deep equality</>
19631994

packages/expect/src/__tests__/matchers.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,13 @@ describe('.toContain(), .toContainEqual()', () => {
14631463

14641464
test('error cases', () => {
14651465
expect(() => jestExpect(null).toContain(1)).toThrowErrorMatchingSnapshot();
1466+
expect(() => jestExpect('-0').toContain(-0)).toThrowErrorMatchingSnapshot();
1467+
expect(() =>
1468+
jestExpect('null').toContain(null),
1469+
).toThrowErrorMatchingSnapshot();
1470+
expect(() =>
1471+
jestExpect('undefined').toContain(undefined),
1472+
).toThrowErrorMatchingSnapshot();
14661473
});
14671474

14681475
[

packages/expect/src/matchers.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,35 @@ const matchers: MatchersObject = {
472472
}
473473

474474
if (typeof received === 'string') {
475+
const wrongTypeErrorMessage = `${EXPECTED_COLOR(
476+
'expected',
477+
)} value must be a string if ${RECEIVED_COLOR(
478+
'received',
479+
)} value is a string`;
480+
481+
if (expected == null) {
482+
throw new Error(
483+
matcherErrorMessage(
484+
matcherHint(matcherName, received, String(expected), options),
485+
wrongTypeErrorMessage,
486+
printWithType('Expected', expected, printExpected) +
487+
'\n' +
488+
printWithType('Received', received, printReceived),
489+
),
490+
);
491+
}
492+
if (typeof expected === 'number') {
493+
throw new Error(
494+
matcherErrorMessage(
495+
matcherHint(matcherName, received, String(expected), options),
496+
wrongTypeErrorMessage,
497+
printWithType('Expected', expected, printExpected) +
498+
'\n' +
499+
printWithType('Received', received, printReceived),
500+
),
501+
);
502+
}
503+
475504
const index = received.indexOf(String(expected));
476505
const pass = index !== -1;
477506

0 commit comments

Comments
 (0)