Skip to content

Commit

Permalink
Just add a message at the bottom instead of replacing it
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeliog committed Aug 17, 2017
1 parent b02325a commit 8cba75e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const NUMBERS = [
'thirteen',
];

const SUGGEST_TO_EQUAL = chalk.dim(
'Looks like you wanted to test for object/array equality with strict `toBe` matcher. You probably need to use `toEqual` instead.',
);

const stringify = (object: any, maxDepth?: number = 10): string => {
const MAX_LENGTH = 10000;
let result;
Expand Down Expand Up @@ -165,6 +169,7 @@ module.exports = {
EXPECTED_COLOR,
RECEIVED_BG,
RECEIVED_COLOR,
SUGGEST_TO_EQUAL,
ensureActualIsNumber,
ensureExpectedIsNumber,
ensureNoExpected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,27 @@ Received:
exports[`.toBe() fails for: [] and [] 1`] = `
"<dim>expect(<red>received</><dim>).toBe(<green>expected</><dim>)

Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
Expected value to be (using ===):
<green>[]</>
Received:
<red>[]</>

Difference:

<dim>Compared values have no visual difference. <dim>Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
`;

exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = `
"<dim>expect(<red>received</><dim>).toBe(<green>expected</><dim>)

Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
Expected value to be (using ===):
<green>{\\"a\\": 1}</>
Received:
<red>{\\"a\\": 1}</>

Difference:

<dim>Compared values have no visual difference. <dim>Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
`;

exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = `
Expand All @@ -263,7 +277,14 @@ Difference:
exports[`.toBe() fails for: {} and {} 1`] = `
"<dim>expect(<red>received</><dim>).toBe(<green>expected</><dim>)

Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
Expected value to be (using ===):
<green>{}</>
Received:
<red>{}</>

Difference:

<dim>Compared values have no visual difference. <dim>Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
`;

exports[`.toBe() fails for: 1 and 2 1`] = `
Expand Down
15 changes: 6 additions & 9 deletions packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

import type {MatchersObject} from 'types/Matchers';

import chalk from 'chalk';
import diff from 'jest-diff';
import getType from 'jest-get-type';
import {escapeStrForRegex} from 'jest-regex-util';
import {
EXPECTED_COLOR,
RECEIVED_COLOR,
SUGGEST_TO_EQUAL,
ensureNoExpected,
ensureNumbers,
matcherHint,
Expand Down Expand Up @@ -67,29 +69,24 @@ const matchers: MatchersObject = {
`Received:\n` +
` ${printReceived(received)}`
: () => {
if (
const suggestToEqual =
getType(received) === getType(expected) &&
(getType(received) === 'object' || getType(expected) === 'array') &&
equals(received, expected, [iterableEquality])
) {
return (
matcherHint('.toBe') +
'\n\n' +
'Looks like you wanted to test for object/array equity with strict `toBe` matcher. You probably need to use `toEqual` instead.'
);
}

const diffString = diff(expected, received, {
expand: this.expand,
});

return (
matcherHint('.toBe') +
'\n\n' +
`Expected value to be (using ===):\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
(suggestToEqual ? ` ${SUGGEST_TO_EQUAL}` : '')
);
};

Expand Down

0 comments on commit 8cba75e

Please sign in to comment.