Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Suggest toContainEqual
  • Loading branch information
mcampagonzalez committed Apr 9, 2018
commit f077b204bbfd170e9a54cf57e818e67cffc9642a
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