Skip to content

Use diff-match-patch for text diffs #1285

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

Merged
merged 2 commits into from
Feb 26, 2017
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
14 changes: 8 additions & 6 deletions lib/format-assert-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const indentString = require('indent-string');
const stripAnsi = require('strip-ansi');
const chalk = require('chalk');
const diff = require('diff');
const DiffMatchPatch = require('diff-match-patch');

const cleanUp = line => {
if (line[0] === '+') {
Expand Down Expand Up @@ -46,18 +47,19 @@ module.exports = err => {
}

if (err.actualType === 'string' && err.expectedType === 'string') {
const patch = diff.diffChars(stripAnsi(err.actual), stripAnsi(err.expected));
const diffMatchPatch = new DiffMatchPatch();
const patch = diffMatchPatch.diff_main(stripAnsi(err.actual), stripAnsi(err.expected));
const msg = patch
.map(part => {
if (part.added) {
return chalk.bgGreen.black(part.value);
if (part[0] === 1) {
return chalk.bgGreen.black(part[1]);
}

if (part.removed) {
return chalk.bgRed.black(part.value);
if (part[0] === -1) {
return chalk.bgRed.black(part[1]);
}

return chalk.red(part.value);
return chalk.red(part[1]);
})
.join('');

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"currently-unhandled": "^0.4.1",
"debug": "^2.2.0",
"diff": "^3.0.1",
"diff-match-patch": "^1.0.0",
"dot-prop": "^4.1.0",
"empower-core": "^0.6.1",
"equal-length": "^1.0.0",
Expand Down