Skip to content

Commit

Permalink
Highlight trailing whitespace inside snapshots (jestjs#2347)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and skovhus committed Apr 29, 2017
1 parent f30d11d commit ae3088a
Showing 1 changed file with 22 additions and 8 deletions.
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

0 comments on commit ae3088a

Please sign in to comment.