Skip to content

Commit

Permalink
Ensure toContain only accepts strings when received is a string (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Dec 8, 2020
1 parent 3a40820 commit 395e93e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
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
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
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
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

0 comments on commit 395e93e

Please sign in to comment.