diff --git a/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap b/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap index d3d7a9edfdc8..641d8cf6218a 100644 --- a/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap +++ b/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap @@ -17,6 +17,76 @@ exports[`collapses big diffs to patch format 1`] = ` }" `; +exports[`context number of lines: -1 (5 default) 1`] = ` +"- Expected ++ Received + +@@ -6,9 +6,9 @@ + 4, + 5, + 6, + 7, + 8, ++ 10, + 9, +- 10, + ], + }" +`; + +exports[`context number of lines: 0 1`] = ` +"- Expected ++ Received + +@@ -11,0 +11,1 @@ ++ 10, +@@ -12,1 +13,0 @@ +- 10," +`; + +exports[`context number of lines: 1 1`] = ` +"- Expected ++ Received + +@@ -10,4 +10,4 @@ + 8, ++ 10, + 9, +- 10, + ]," +`; + +exports[`context number of lines: 2 1`] = ` +"- Expected ++ Received + +@@ -9,6 +9,6 @@ + 7, + 8, ++ 10, + 9, +- 10, + ], + }" +`; + +exports[`context number of lines: null (5 default) 1`] = ` +"- Expected ++ Received + +@@ -6,9 +6,9 @@ + 4, + 5, + 6, + 7, + 8, ++ 10, + 9, +- 10, + ], + }" +`; + exports[`falls back to not call toJSON if objects look identical 1`] = ` "Compared values serialize to the same structure. Printing internal object structure without calling \`toJSON\` instead. diff --git a/packages/jest-diff/src/__tests__/diff.test.js b/packages/jest-diff/src/__tests__/diff.test.js index a820c9eca493..ca0956c9d500 100644 --- a/packages/jest-diff/src/__tests__/diff.test.js +++ b/packages/jest-diff/src/__tests__/diff.test.js @@ -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)' + : ''}`, () => { + 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 +}); diff --git a/packages/jest-diff/src/diff_strings.js b/packages/jest-diff/src/diff_strings.js index 4e3b0dcafdc7..50da0c69b30f 100644 --- a/packages/jest-diff/src/diff_strings.js +++ b/packages/jest-diff/src/diff_strings.js @@ -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}; @@ -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')) { @@ -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) {