From e4dd7641017551b18efc77a04f6f172ca64104e7 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 2 Sep 2019 15:17:41 -0400 Subject: [PATCH 1/2] jest-diff: Do not inverse format if line consists of one change --- .../__snapshots__/matchers.test.js.snap | 6 ++-- .../getAlignedDiffs.test.ts.snap | 36 +++++++++---------- packages/jest-diff/src/getAlignedDiffs.ts | 10 +++++- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 9c018a897cee..736c6547c1e9 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -303,9 +303,9 @@ string" 1`] = ` - Expected + Received -- 3 -+ four -+ 4 +- 3 ++ four ++ 4 line string" `; diff --git a/packages/jest-diff/src/__tests__/__snapshots__/getAlignedDiffs.test.ts.snap b/packages/jest-diff/src/__tests__/__snapshots__/getAlignedDiffs.test.ts.snap index bc62b6fff2c1..48f282bf8a3e 100644 --- a/packages/jest-diff/src/__tests__/__snapshots__/getAlignedDiffs.test.ts.snap +++ b/packages/jest-diff/src/__tests__/__snapshots__/getAlignedDiffs.test.ts.snap @@ -1,34 +1,34 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`getAlignedDiffs lines change preceding and following common 1`] = ` -"- delete -+ insert +"- delete ++ insert common between changes -- prev -+ next" +- prev ++ next" `; exports[`getAlignedDiffs lines common at end when both current change lines are empty 1`] = ` -"- delete +"- delete common at end" `; exports[`getAlignedDiffs lines common between delete and insert 1`] = ` -"- delete +"- delete common between changes -+ insert" ++ insert" `; exports[`getAlignedDiffs lines common between insert and delete 1`] = ` -"+ insert +"+ insert common between changes -- delete" +- delete" `; exports[`getAlignedDiffs lines common preceding and following change 1`] = ` " common preceding -- delete -+ insert +- delete ++ insert common following" `; @@ -85,7 +85,7 @@ exports[`getAlignedDiffs strings delete or insert at start and change at end 1`] `; exports[`getAlignedDiffs substrings first common when both current change lines are empty 1`] = ` -"+ insert +"+ insert first middle - last prev @@ -101,7 +101,7 @@ exports[`getAlignedDiffs substrings first common when either current change line exports[`getAlignedDiffs substrings first delete completes the current line 1`] = ` "- common preceding first -- middle +- middle - last and following + common preceding and following" `; @@ -109,7 +109,7 @@ exports[`getAlignedDiffs substrings first delete completes the current line 1`] exports[`getAlignedDiffs substrings first insert completes the current line 1`] = ` "- common preceding + common preceding first -+ middle ++ middle +" `; @@ -140,21 +140,21 @@ exports[`getAlignedDiffs substrings middle is empty in delete between common 1`] exports[`getAlignedDiffs substrings middle is empty in insert at start 1`] = ` "- expected common at end -+ insert line ++ insert line + + received common at end" `; exports[`getAlignedDiffs substrings middle is non-empty in delete at end 1`] = ` "- common at start precedes delete -- non-empty line -- next +- non-empty line +- next + common at start precedes prev" `; exports[`getAlignedDiffs substrings middle is non-empty in insert between common 1`] = ` "- common at start precedes delete expected + common at start precedes insert -+ non-empty ++ non-empty + received" `; diff --git a/packages/jest-diff/src/getAlignedDiffs.ts b/packages/jest-diff/src/getAlignedDiffs.ts index 19421ae5e06a..2dae77412fd0 100644 --- a/packages/jest-diff/src/getAlignedDiffs.ts +++ b/packages/jest-diff/src/getAlignedDiffs.ts @@ -27,8 +27,16 @@ class ChangeBuffer { private pushLine(): void { // Assume call only if line has at least one diff, // therefore an empty line must have a diff which has an empty string. + + // If line has multiple diffs, then assume it has a common diff, + // therefore change diffs have change color; + // otherwise then it has line color only. this.lines.push( - new Diff(this.op, invertChangedSubstrings(this.op, this.line)), + this.line.length !== 1 + ? new Diff(this.op, invertChangedSubstrings(this.op, this.line)) + : this.line[0][0] === this.op + ? this.line[0] // can use instance + : new Diff(this.op, this.line[0][1]), // was common diff ); this.line.length = 0; } From f4e3794c0fc34cac87f5478cf682f97af64e09ba Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 2 Sep 2019 15:25:21 -0400 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8139630c88..fc78d401485a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### Fixes - `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389)) +- `[jest-diff]` Do not inverse format if line consists of one change ([#8903](https://github.com/facebook/jest/pull/8903)) - `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764)) - `[jest-leak-detector]` [**BREAKING**] Use `weak-napi` instead of `weak` package ([#8686](https://github.com/facebook/jest/pull/8686)) - `[jest-mock]` Fix for mockReturnValue overriding mockImplementationOnce ([#8398](https://github.com/facebook/jest/pull/8398))