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
Follow-up of jestjs#10119
  • Loading branch information
dubzzz committed Dec 8, 2020
1 parent 3a40820 commit 59274dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
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

0 comments on commit 59274dd

Please sign in to comment.