From 8852ce51d5dad3ae4abd4bc2673f9e4af111474f Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 1 Oct 2017 15:31:57 -0400 Subject: [PATCH] Call chalk.inverse for trailing spaces in jest-matcher-utils --- .../__tests__/__snapshots__/matchers.test.js.snap | 11 +++++++++++ packages/expect/src/__tests__/matchers.test.js | 1 + packages/expect/src/to_throw_matchers.js | 3 +-- packages/jest-matcher-utils/src/index.js | 12 ++++-------- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 1c7e541cdad8..c98f2be7cb3a 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -229,6 +229,17 @@ Received: \\"abc\\"" `; +exports[`.toBe() fails for: "with +trailing space" and "without trailing space" 1`] = ` +"expect(received).toBe(expected) + +Expected value to be (using ===): + \\"without trailing space\\" +Received: + \\"with +trailing space\\"" +`; + exports[`.toBe() fails for: [] and [] 1`] = ` "expect(received).toBe(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index e19cd982b900..4fbb763be944 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -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]) => { diff --git a/packages/expect/src/to_throw_matchers.js b/packages/expect/src/to_throw_matchers.js index 60231cf7bcf0..3a448df231d6 100644 --- a/packages/expect/src/to_throw_matchers.js +++ b/packages/expect/src/to_throw_matchers.js @@ -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, @@ -173,7 +172,7 @@ const printActualErrorMessage = error => { `Instead, it threw:\n` + RECEIVED_COLOR( ' ' + - highlightTrailingWhitespace(message, RECEIVED_BG) + + highlightTrailingWhitespace(message) + formatStackTrace( stack, { diff --git a/packages/jest-matcher-utils/src/index.js b/packages/jest-matcher-utils/src/index.js index c0f99dc331aa..89f2442880e5 100644 --- a/packages/jest-matcher-utils/src/index.js +++ b/packages/jest-matcher-utils/src/index.js @@ -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', @@ -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,