Skip to content

Commit

Permalink
Make toContain more strict with the received type
Browse files Browse the repository at this point in the history
Fixes #8023
  • Loading branch information
ghostd committed Jun 2, 2020
1 parent 81712ba commit 8609c5c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,37 @@ exports[`.toContain(), .toContainEqual() error cases 1`] = `
Received has value: <r>null</>
`;

exports[`.toContain(), .toContainEqual() error cases 2`] = `
<d>expect(</><r>-0</><d>).</>toContain<d>(</><g>0</><d>) // indexOf</>

<b>Matcher error</>: <g>expected</> value must be a string if <r>received</> value is a string

Expected has type: number
Expected has value: <g>-0</>
Received has type: string
Received has value: <r>"-0"</>
`;

exports[`.toContain(), .toContainEqual() error cases 3`] = `
<d>expect(</><r>null</><d>).</>toContain<d>(</><g>null</><d>) // indexOf</>

<b>Matcher error</>: <g>expected</> value must be a string if <r>received</> value is a string

Expected has value: <g>null</>
Received has type: string
Received has value: <r>"null"</>
`;

exports[`.toContain(), .toContainEqual() error cases 4`] = `
<d>expect(</><r>undefined</><d>).</>toContain<d>(</><g>undefined</><d>) // indexOf</>

<b>Matcher error</>: <g>expected</> value must be a string if <r>received</> value is a string

Expected has value: <g>undefined</>
Received has type: string
Received has value: <r>"undefined"</>
`;

exports[`.toContain(), .toContainEqual() error cases for toContainEqual 1`] = `
<d>expect(</><r>received</><d>).</>toContainEqual<d>(</><g>expected</><d>) // deep equality</>

Expand Down
7 changes: 7 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

[
Expand Down
29 changes: 29 additions & 0 deletions packages/expect/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 8609c5c

Please sign in to comment.