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 trailing whitespace inside snapshots #2347

Merged
Merged
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
30 changes: 22 additions & 8 deletions packages/jest-diff/src/diffStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export type DiffOptions = {|

type Diff = {diff: string, isDifferent: boolean};

const getColor = (added: boolean, removed: boolean): chalk =>
added
? chalk.red
: (removed ? chalk.green : chalk.dim);

const getBgColor = (added: boolean, removed: boolean): chalk =>
added
? chalk.bgRed
: (removed ? chalk.bgGreen : chalk.dim);

const highlightTrailingWhitespace = (line: string, bgColor: Function): string =>
line.replace(/\s+$/, bgColor('$&'));

const getAnnotation = (options: ?DiffOptions): string =>
chalk.green('- ' + ((options && options.aAnnotation) || 'Expected')) + '\n' +
chalk.red('+ ' + ((options && options.bAnnotation) || 'Received')) + '\n\n';
Expand All @@ -32,22 +45,23 @@ const diffLines = (a: string, b: string): Diff => {
let isDifferent = false;
return {
diff: diff.diffLines(a, b).map(part => {
const {added, removed} = part;
if (part.added || part.removed) {
isDifferent = true;
}

const lines = part.value.split('\n');
const color = part.added
? chalk.red
: (part.removed ? chalk.green : chalk.dim);
const color = getColor(added, removed);
const bgColor = getBgColor(added, removed);

if (lines[lines.length - 1] === '') {
lines.pop();
}

return lines.map(line => {
const highlightedLine = highlightTrailingWhitespace(line, bgColor);
const mark = color(part.added ? '+' : part.removed ? '-' : ' ');
return mark + ' ' + color(line) + '\n';
return mark + ' ' + color(highlightedLine) + '\n';
}).join('');
}).join('').trim(),
isDifferent,
Expand Down Expand Up @@ -76,11 +90,11 @@ const structuredPatch = (a: string, b: string): Diff => {
const added = line[0] === '+';
const removed = line[0] === '-';

const color = added
? chalk.red
: (removed ? chalk.green : chalk.dim);
const color = getColor(added, removed);
const bgColor = getBgColor(added, removed);

return color(line) + '\n';
const highlightedLine = highlightTrailingWhitespace(line, bgColor);
return color(highlightedLine) + '\n';
}).join('');

isDifferent = true;
Expand Down