Skip to content

Commit

Permalink
Highlight whitespace in expected/received (jestjs#2674)
Browse files Browse the repository at this point in the history
* Highlight whitespace in expected/received

* Simplify text replacement with multiline regexp
  • Loading branch information
thymikee authored and skovhus committed Apr 29, 2017
1 parent 75613f1 commit 9fb0349
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export type ValueType =
| 'undefined';

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

const NUMBERS = [
'zero',
Expand Down Expand Up @@ -110,8 +112,17 @@ const stringify = (object: any, maxDepth?: number = 10): string => {
: result;
};

const printReceived = (object: any) => RECEIVED_COLOR(stringify(object));
const printExpected = (value: any) => EXPECTED_COLOR(stringify(value));
const highlightTrailingWhitespace = (text: string, bgColor: Function): string =>
text.replace(/\s+$/gm, bgColor('$&'));

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

const printWithType = (
name: string,
Expand Down Expand Up @@ -192,13 +203,16 @@ const matcherHint = (
};

module.exports = {
EXPECTED_BG,
EXPECTED_COLOR,
RECEIVED_BG,
RECEIVED_COLOR,
ensureActualIsNumber,
ensureExpectedIsNumber,
ensureNoExpected,
ensureNumbers,
getType,
highlightTrailingWhitespace,
matcherHint,
pluralize,
printExpected,
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-matchers/src/toThrowMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ const {
separateMessageFromStack,
} = require('jest-util');
const {
RECEIVED_BG,
RECEIVED_COLOR,
getType,
highlightTrailingWhitespace,
matcherHint,
printExpected,
printWithType,
Expand Down Expand Up @@ -149,7 +151,9 @@ const printActualErrorMessage = error => {
return (
`Instead, it threw:\n` +
RECEIVED_COLOR(
' ' + message + formatStackTrace(stack, {
' ' +
highlightTrailingWhitespace(message, RECEIVED_BG) +
formatStackTrace(stack, {
noStackTrace: false,
rootDir: process.cwd(),
testRegex: '',
Expand Down

0 comments on commit 9fb0349

Please sign in to comment.