Skip to content

Commit 8020c31

Browse files
authored
Make toContain more strict with the received type (#10119)
1 parent 8a5c4d5 commit 8020c31

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903))
1818
- `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708))
19+
- `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119))
1920
- `[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)
2021
- `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871))
2122
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))

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
@@ -474,6 +474,35 @@ const matchers: MatchersObject = {
474474
}
475475

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

0 commit comments

Comments
 (0)