diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fc18fb8af4e..e911716f6511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index cc7fc3b606e9..e77f3ee64c5a 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1517,7 +1517,7 @@ exports[`.toContain(), .toContainEqual() '[{}, []]' does not contain '[]' 1`] = Expected array: [{}, []] To contain value: - []" + [] 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`] = ` @@ -1526,7 +1526,7 @@ exports[`.toContain(), .toContainEqual() '[{}, []]' does not contain '{}' 1`] = Expected array: [{}, []] To contain value: - {}" + {} 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`] = ` diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 6a81a1faa3e6..773502f7407b 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -16,6 +16,7 @@ import { EXPECTED_COLOR, RECEIVED_COLOR, SUGGEST_TO_EQUAL, + SUGGEST_TO_CONTAIN_EQUAL, ensureNoExpected, ensureNumbers, matcherHint, @@ -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}; }, diff --git a/packages/jest-matcher-utils/src/index.js b/packages/jest-matcher-utils/src/index.js index 557dd9cba5ca..8123394744dc 100644 --- a/packages/jest-matcher-utils/src/index.js +++ b/packages/jest-matcher-utils/src/index.js @@ -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;