Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure toContain only accepts strings when received is a string #10929

Merged
merged 2 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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))
- `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929))
- `[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
11 changes: 11 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,17 @@ Received has type: string
Received has value: <r>"undefined"</>
`;

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

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

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

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

Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,14 @@ describe('.toContain(), .toContainEqual()', () => {
expect(() =>
jestExpect('undefined').toContain(undefined),
).toThrowErrorMatchingSnapshot();
expect(() =>
jestExpect('false').toContain(false),
).toThrowErrorMatchingSnapshot();
if (isBigIntDefined) {
expect(() => jestExpect('1').toContain(BigInt(1))).toThrowError(
'toContain',
);
}
});

[
Expand Down
13 changes: 1 addition & 12 deletions packages/expect/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,7 @@ const matchers: MatchersObject = {
'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') {
if (typeof expected !== 'string') {
throw new Error(
matcherErrorMessage(
matcherHint(matcherName, received, String(expected), options),
Expand Down