Skip to content

Commit

Permalink
jest-diff: Do not inverse format if line consists of one change (#8903)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and SimenB committed Sep 4, 2019
1 parent c588c18 commit 8e0786f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ string" 1`] = `
<green>- Expected</>
<red>+ Received</>

<green>- <inverse>3</></>
<red>+ <inverse>four</></>
<red>+ <inverse>4</></>
<green>- 3</>
<red>+ four</>
<red>+ 4</>
<dim> line</>
<dim> string</>"
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getAlignedDiffs lines change preceding and following common 1`] = `
"<green>- <inverse>delete</></>
<red>+ <inverse>insert</></>
"<green>- delete</>
<red>+ insert</>
<dim> common between changes</>
<green>- <inverse>prev</></>
<red>+ <inverse>next</></>"
<green>- prev</>
<red>+ next</>"
`;
exports[`getAlignedDiffs lines common at end when both current change lines are empty 1`] = `
"<green>- <inverse>delete</></>
"<green>- delete</>
<dim> common at end</>"
`;
exports[`getAlignedDiffs lines common between delete and insert 1`] = `
"<green>- <inverse>delete</></>
"<green>- delete</>
<dim> common between changes</>
<red>+ <inverse>insert</></>"
<red>+ insert</>"
`;
exports[`getAlignedDiffs lines common between insert and delete 1`] = `
"<red>+ <inverse>insert</></>
"<red>+ insert</>
<dim> common between changes</>
<green>- <inverse>delete</></>"
<green>- delete</>"
`;
exports[`getAlignedDiffs lines common preceding and following change 1`] = `
"<dim> common preceding</>
<green>- <inverse>delete</></>
<red>+ <inverse>insert</></>
<green>- delete</>
<red>+ insert</>
<dim> common following</>"
`;
Expand Down Expand Up @@ -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`] = `
"<red>+ <inverse>insert</></>
"<red>+ insert</>
<dim> first</>
<dim> middle</>
<green>- last <inverse>prev</></>
Expand All @@ -101,15 +101,15 @@ exports[`getAlignedDiffs substrings first common when either current change line
exports[`getAlignedDiffs substrings first delete completes the current line 1`] = `
"<green>- common preceding <inverse>first</></>
<green>- <inverse>middle</></>
<green>- middle</>
<green>- <inverse>last </>and following</>
<red>+ common preceding and following</>"
`;
exports[`getAlignedDiffs substrings first insert completes the current line 1`] = `
"<green>- common preceding</>
<red>+ common preceding<inverse> first</></>
<red>+ <inverse>middle</></>
<red>+ middle</>
<red>+</>"
`;
Expand Down Expand Up @@ -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`] = `
"<green>- <inverse>expect</>ed common at end</>
<red>+ <inverse>insert line</></>
<red>+ insert line</>
<red>+</>
<red>+ <inverse>receiv</>ed common at end</>"
`;
exports[`getAlignedDiffs substrings middle is non-empty in delete at end 1`] = `
"<green>- common at start precedes <inverse>delete</></>
<green>- <inverse>non-empty line</></>
<green>- <inverse>next</></>
<green>- non-empty line</>
<green>- next</>
<red>+ common at start precedes <inverse>prev</></>"
`;
exports[`getAlignedDiffs substrings middle is non-empty in insert between common 1`] = `
"<green>- common at start precedes <inverse>delete expect</>ed</>
<red>+ common at start precedes <inverse>insert</></>
<red>+ <inverse>non-empty</></>
<red>+ non-empty</>
<red>+ <inverse>receiv</>ed</>"
`;
10 changes: 9 additions & 1 deletion packages/jest-diff/src/getAlignedDiffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 8e0786f

Please sign in to comment.