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

Add contextLines option to jest-diff #4152

Merged
merged 2 commits into from
Jul 28, 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
70 changes: 70 additions & 0 deletions packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,76 @@ exports[`collapses big diffs to patch format 1`] = `
<dim> }"
`;

exports[`context number of lines: -1 (5 default) 1`] = `
"<green>- Expected</>
<red>+ Received</>

@@ -6,9 +6,9 @@</>
</><dim> 4,
<dim> 5,
<dim> 6,
<dim> 7,
<dim> 8,
<red>+ 10,</>
<dim> 9,
<green>- 10,</>
<dim> ],
<dim> }"
`;

exports[`context number of lines: 0 1`] = `
"<green>- Expected</>
<red>+ Received</>

@@ -11,0 +11,1 @@</>
</><red>+ 10,</>
@@ -12,1 +13,0 @@</>
</><green>- 10,</>"
`;

exports[`context number of lines: 1 1`] = `
"<green>- Expected</>
<red>+ Received</>

@@ -10,4 +10,4 @@</>
</><dim> 8,
<red>+ 10,</>
<dim> 9,
<green>- 10,</>
<dim> ],"
`;

exports[`context number of lines: 2 1`] = `
"<green>- Expected</>
<red>+ Received</>

@@ -9,6 +9,6 @@</>
</><dim> 7,
<dim> 8,
<red>+ 10,</>
<dim> 9,
<green>- 10,</>
<dim> ],
<dim> }"
`;

exports[`context number of lines: null (5 default) 1`] = `
"<green>- Expected</>
<red>+ Received</>

@@ -6,9 +6,9 @@</>
</><dim> 4,
<dim> 5,
<dim> 6,
<dim> 7,
<dim> 8,
<red>+ 10,</>
<dim> 9,
<green>- 10,</>
<dim> ],
<dim> }"
`;

exports[`falls back to not call toJSON if objects look identical 1`] = `
"<dim>Compared values serialize to the same structure.
<dim>Printing internal object structure without calling \`toJSON\` instead.
Expand Down
26 changes: 26 additions & 0 deletions packages/jest-diff/src/__tests__/diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,29 @@ test('collapses big diffs to patch format', () => {

expect(result).toMatchSnapshot();
});

describe('context', () => {
const testDiffContextLines = (contextLines?: number) => {
test(`number of lines: ${typeof contextLines === 'number'
? contextLines
: 'null'} ${typeof contextLines !== 'number' || contextLines < 0
? '(5 default)'
: ''}`, () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need to run prettier

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, this is OK. But maybe we can make it more readable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flow complains if I extract it and I did not simplify it because 0 is falsy too.

const result = diff(
{test: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]},
{test: [1, 2, 3, 4, 5, 6, 7, 8, 10, 9]},
{
contextLines,
expand: false,
},
);
expect(result).toMatchSnapshot();
});
};

testDiffContextLines(); // 5 by default
testDiffContextLines(2);
testDiffContextLines(1);
testDiffContextLines(0);
testDiffContextLines(-1); // Will use default
});
14 changes: 10 additions & 4 deletions packages/jest-diff/src/diff_strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import {
} from 'diff';

import {NO_DIFF_MESSAGE} from './constants.js';
const DIFF_CONTEXT = 5;
const DIFF_CONTEXT_DEFAULT = 5;

export type DiffOptions = {|
aAnnotation?: string,
bAnnotation?: string,
expand?: boolean,
contextLines?: number,
|};

type Diff = {diff: string, isDifferent: boolean};
Expand Down Expand Up @@ -94,8 +95,13 @@ const createPatchMark = (hunk: Hunk): string => {
return chalk.yellow(`@@ ${markOld} ${markNew} @@\n`);
};

const structuredPatch = (a: string, b: string): Diff => {
const options = {context: DIFF_CONTEXT};
const structuredPatch = (a: string, b: string, contextLines?: number): Diff => {
const options = {
context:
typeof contextLines === 'number' && contextLines >= 0
? contextLines
: DIFF_CONTEXT_DEFAULT,
};
let isDifferent = false;
// Make sure the strings end with a newline.
if (!a.endsWith('\n')) {
Expand Down Expand Up @@ -141,7 +147,7 @@ function diffStrings(a: string, b: string, options: ?DiffOptions): string {
// whenever linebreaks are involved.
const result =
options && options.expand === false
? structuredPatch(a, b)
? structuredPatch(a, b, options && options.contextLines)
: diffLines(a, b);

if (result.isDifferent) {
Expand Down