Skip to content
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

jest-diff: Do not inverse format if line consists of one change #8903

Merged
merged 2 commits into from
Sep 4, 2019
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
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