From eca5942a3cff81e5e6d866050dd778c001736aae Mon Sep 17 00:00:00 2001 From: Alexander Tesfamichael Date: Mon, 1 May 2017 21:18:59 +0200 Subject: [PATCH 1/4] Add diff-match-patch dependency --- packages/jest-diff/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-diff/package.json b/packages/jest-diff/package.json index 51d0800df18c..ad1a4a9f8380 100644 --- a/packages/jest-diff/package.json +++ b/packages/jest-diff/package.json @@ -10,6 +10,7 @@ "dependencies": { "chalk": "^1.1.3", "diff": "^3.2.0", + "diff-match-patch": "^1.0.0", "jest-matcher-utils": "^19.0.0", "pretty-format": "^19.0.0" } From ab34fc5ede439eb66a5e56c15e3a6dd2d3a63212 Mon Sep 17 00:00:00 2001 From: Alexander Tesfamichael Date: Mon, 1 May 2017 21:22:52 +0200 Subject: [PATCH 2/4] Use descriptive aliases consistently --- packages/jest-diff/src/diffStrings.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/jest-diff/src/diffStrings.js b/packages/jest-diff/src/diffStrings.js index 787d930eb680..b18b6abf48d9 100644 --- a/packages/jest-diff/src/diffStrings.js +++ b/packages/jest-diff/src/diffStrings.js @@ -54,11 +54,13 @@ const diffLines = (a: string, b: string): Diff => { .diffLines(a, b) .map(part => { const {added, removed} = part; - if (part.added || part.removed) { + const value = part.value; + + if (added || removed) { isDifferent = true; } - const lines = part.value.split('\n'); + const lines = value.split('\n'); const color = getColor(added, removed); const bgColor = getBgColor(added, removed); @@ -69,7 +71,7 @@ const diffLines = (a: string, b: string): Diff => { return lines .map(line => { const highlightedLine = highlightTrailingWhitespace(line, bgColor); - const mark = color(part.added ? '+' : part.removed ? '-' : ' '); + const mark = color(added ? '+' : removed ? '-' : ' '); return mark + ' ' + color(highlightedLine) + '\n'; }) .join(''); From 6d1c9dd1596cb95eb5daac4d9ced883eae8b9b3f Mon Sep 17 00:00:00 2001 From: Alexander Tesfamichael Date: Mon, 1 May 2017 21:23:36 +0200 Subject: [PATCH 3/4] Swap out diff.diffLines for diffMatchPatch --- packages/jest-diff/src/diffStrings.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/jest-diff/src/diffStrings.js b/packages/jest-diff/src/diffStrings.js index b18b6abf48d9..b66b4051ce2d 100644 --- a/packages/jest-diff/src/diffStrings.js +++ b/packages/jest-diff/src/diffStrings.js @@ -12,6 +12,7 @@ const chalk = require('chalk'); const diff = require('diff'); +const DiffMatchPatch = require('diff-match-patch'); const {NO_DIFF_MESSAGE} = require('./constants.js'); const DIFF_CONTEXT = 5; @@ -46,15 +47,17 @@ const getAnnotation = (options: ?DiffOptions): string => '\n' + chalk.red('+ ' + ((options && options.bAnnotation) || 'Received')) + '\n\n'; - +const diffMatchPatch = new DiffMatchPatch(); +diffMatchPatch.Diff_Timeout = 0; const diffLines = (a: string, b: string): Diff => { let isDifferent = false; + const diffs = diffMatchPatch.diff_main(a, b); return { - diff: diff - .diffLines(a, b) + diff: diffs .map(part => { - const {added, removed} = part; - const value = part.value; + const added = part[0] === 1; + const removed = part[0] === -1; + const value = part[1]; if (added || removed) { isDifferent = true; From dba2054e484e9bff699a2fffb5bc035bbd5f4579 Mon Sep 17 00:00:00 2001 From: Alexander Tesfamichael Date: Mon, 1 May 2017 21:24:05 +0200 Subject: [PATCH 4/4] WIP: try and get correct diff back --- packages/jest-diff/src/diffStrings.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/jest-diff/src/diffStrings.js b/packages/jest-diff/src/diffStrings.js index b66b4051ce2d..ebe3cfb89714 100644 --- a/packages/jest-diff/src/diffStrings.js +++ b/packages/jest-diff/src/diffStrings.js @@ -52,6 +52,8 @@ diffMatchPatch.Diff_Timeout = 0; const diffLines = (a: string, b: string): Diff => { let isDifferent = false; const diffs = diffMatchPatch.diff_main(a, b); + // the following step likely needs changing + diffMatchPatch.diff_cleanupSemantic(diffs); return { diff: diffs .map(part => {