Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight whitespace in expected/received #2674

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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