Skip to content

Commit

Permalink
Suggest toContainEqual (#5953)
Browse files Browse the repository at this point in the history
* Suggest toContainEqual

* Update changelog

* Fix package name in CHANGELOG
  • Loading branch information
mcampagonzalez authored and cpojer committed Apr 12, 2018
1 parent a7a65e2 commit a0bf957
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

* `[expect]` Suggest toContainEqual
([#5948](https://github.com/facebook/jest/pull/5953))
* `[jest-config]` Export Jest's default options
([#5948](https://github.com/facebook/jest/pull/5948))
* `[jest-editor-support]` Move `coverage` to `ProjectWorkspace.collectCoverage`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ exports[`.toContain(), .toContainEqual() '[{}, []]' does not contain '[]' 1`] =
Expected array:
<red>[{}, []]</>
To contain value:
<green>[]</>"
<green>[]</> <dim>Looks like you wanted to test for object/array equality with the stricter \`toContain\` matcher. You probably need to use \`toContainEqual\` instead.</>"
`;

exports[`.toContain(), .toContainEqual() '[{}, []]' does not contain '{}' 1`] = `
Expand All @@ -1526,7 +1526,7 @@ exports[`.toContain(), .toContainEqual() '[{}, []]' does not contain '{}' 1`] =
Expected array:
<red>[{}, []]</>
To contain value:
<green>{}</>"
<green>{}</> <dim>Looks like you wanted to test for object/array equality with the stricter \`toContain\` matcher. You probably need to use \`toContainEqual\` instead.</>"
`;

exports[`.toContain(), .toContainEqual() '[0, 1]' contains '1' 1`] = `
Expand Down
26 changes: 19 additions & 7 deletions packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
EXPECTED_COLOR,
RECEIVED_COLOR,
SUGGEST_TO_EQUAL,
SUGGEST_TO_CONTAIN_EQUAL,
ensureNoExpected,
ensureNumbers,
matcherHint,
Expand Down Expand Up @@ -297,13 +298,24 @@ const matchers: MatchersObject = {
` ${printReceived(collection)}\n` +
`Not to contain value:\n` +
` ${printExpected(value)}\n`
: () =>
matcherHint('.toContain', collectionType, 'value') +
'\n\n' +
`Expected ${collectionType}:\n` +
` ${printReceived(collection)}\n` +
`To contain value:\n` +
` ${printExpected(value)}`;
: () => {
const suggestToContainEqual =
converted !== null &&
typeof converted !== 'string' &&
converted instanceof Array &&
converted.findIndex(item =>
equals(item, value, [iterableEquality]),
) !== -1;
return (
matcherHint('.toContain', collectionType, 'value') +
'\n\n' +
`Expected ${collectionType}:\n` +
` ${printReceived(collection)}\n` +
`To contain value:\n` +
` ${printExpected(value)}` +
(suggestToContainEqual ? ` ${SUGGEST_TO_CONTAIN_EQUAL}` : '')
);
};

return {message, pass};
},
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export const SUGGEST_TO_EQUAL = chalk.dim(
'Looks like you wanted to test for object/array equality with the stricter `toBe` matcher. You probably need to use `toEqual` instead.',
);

export const SUGGEST_TO_CONTAIN_EQUAL = chalk.dim(
'Looks like you wanted to test for object/array equality with the stricter `toContain` matcher. You probably need to use `toContainEqual` instead.',
);

export const stringify = (object: any, maxDepth?: number = 10): string => {
const MAX_LENGTH = 10000;
let result;
Expand Down

0 comments on commit a0bf957

Please sign in to comment.