Skip to content

Commit

Permalink
Make toContain more strict with the received type (#10119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostd committed Dec 5, 2020
1 parent 8a5c4d5 commit 8020c31
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@

- `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903))
- `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708))
- `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119))
- `[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)
- `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871))
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
Expand Down
31 changes: 31 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
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
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
Expand Up @@ -474,6 +474,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 8020c31

Please sign in to comment.