Skip to content

Commit

Permalink
Call chalk.inverse for trailing spaces in jest-matcher-utils (jestjs#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and tabrindle committed Oct 2, 2017
1 parent 65f40cb commit fece5dc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 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 @@ -229,6 +229,17 @@ Received:
<red>\\"abc\\"</>"
`;

exports[`.toBe() fails for: "with
trailing space" and "without trailing space" 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
<green>\\"without trailing space\\"</>
Received:
<red>\\"with<inverse> </></>
<red>trailing space\\"</>"
`;

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

Expand Down
1 change: 1 addition & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe('.toBe()', () => {
[{a: 1}, {a: 1}],
[{a: 1}, {a: 5}],
['abc', 'cde'],
['with \ntrailing space', 'without trailing space'],
[[], []],
[null, undefined],
].forEach(([a, b]) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/expect/src/to_throw_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import getType from 'jest-get-type';
import {escapeStrForRegex} from 'jest-regex-util';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
import {
RECEIVED_BG,
RECEIVED_COLOR,
highlightTrailingWhitespace,
matcherHint,
Expand Down Expand Up @@ -173,7 +172,7 @@ const printActualErrorMessage = error => {
`Instead, it threw:\n` +
RECEIVED_COLOR(
' ' +
highlightTrailingWhitespace(message, RECEIVED_BG) +
highlightTrailingWhitespace(message) +
formatStackTrace(
stack,
{
Expand Down
12 changes: 4 additions & 8 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ const PLUGINS = [
];

export const EXPECTED_COLOR = chalk.green;
export const EXPECTED_BG = chalk.bgGreen;
export const RECEIVED_COLOR = chalk.red;
export const RECEIVED_BG = chalk.bgRed;

const NUMBERS = [
'zero',
Expand Down Expand Up @@ -76,15 +74,13 @@ export const stringify = (object: any, maxDepth?: number = 10): string => {
: result;
};

export const highlightTrailingWhitespace = (
text: string,
bgColor: Function,
): string => text.replace(/\s+$/gm, bgColor('$&'));
export const highlightTrailingWhitespace = (text: string): string =>
text.replace(/\s+$/gm, chalk.inverse('$&'));

export const printReceived = (object: any) =>
highlightTrailingWhitespace(RECEIVED_COLOR(stringify(object)), RECEIVED_BG);
RECEIVED_COLOR(highlightTrailingWhitespace(stringify(object)));
export const printExpected = (value: any) =>
highlightTrailingWhitespace(EXPECTED_COLOR(stringify(value)), EXPECTED_BG);
EXPECTED_COLOR(highlightTrailingWhitespace(stringify(value)));

export const printWithType = (
name: string,
Expand Down

0 comments on commit fece5dc

Please sign in to comment.