From 13001eb52752024ca482e67f6c141b1412855fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 30 Jan 2018 22:56:07 +0100 Subject: [PATCH 1/3] Simplify test Component --- __tests__/snapshotDiff.test.js | 70 +++------------------------------- 1 file changed, 6 insertions(+), 64 deletions(-) diff --git a/__tests__/snapshotDiff.test.js b/__tests__/snapshotDiff.test.js index 29ce66b..f71c9e9 100644 --- a/__tests__/snapshotDiff.test.js +++ b/__tests__/snapshotDiff.test.js @@ -37,6 +37,10 @@ type Props = { class Component extends React.Component { render() { + const dummySpans = Array(20) + .fill(0) + .map((_, index) => ); + return (
@@ -44,71 +48,9 @@ class Component extends React.Component { {this.props.test} {this.props.test} - - - - - - - - - - - - - - - - - - - - + {dummySpans} {this.props.test} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + {dummySpans}
); } From cb3544298dc157de786c1bdcc18265b8f3654c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 30 Jan 2018 23:07:53 +0100 Subject: [PATCH 2/3] fix: Support diffing single-line strings --- __tests__/__snapshots__/snapshotDiff.test.js.snap | 10 ++++++++++ __tests__/snapshotDiff.test.js | 4 ++++ src/index.js | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/__tests__/__snapshots__/snapshotDiff.test.js.snap b/__tests__/__snapshots__/snapshotDiff.test.js.snap index b3b9d94..6a068af 100644 --- a/__tests__/__snapshots__/snapshotDiff.test.js.snap +++ b/__tests__/__snapshots__/snapshotDiff.test.js.snap @@ -134,3 +134,13 @@ exports[`failed optional deps throws with sensible message on missing react-test "Failed to load optional module \\"react-test-renderer\\". If you need to compare React elements, please add \\"react-test-renderer\\" to your project's dependencies. Cannot find module 'non-existent-module-for-testing' from 'snapshotDiff.test.js'" `; + +exports[`supports diffing single-line strings 1`] = ` +"Snapshot Diff: +- First value ++ Second value + +- foo ++ bar + " +`; diff --git a/__tests__/snapshotDiff.test.js b/__tests__/snapshotDiff.test.js index f71c9e9..a7a185d 100644 --- a/__tests__/snapshotDiff.test.js +++ b/__tests__/snapshotDiff.test.js @@ -56,6 +56,10 @@ class Component extends React.Component { } } +test('supports diffing single-line strings', () => { + expect(snapshotDiff('foo', 'bar')).toMatchSnapshot(); +}); + test('collapses diffs and strips ansi by default', () => { expect(snapshotDiff(a, b)).toMatchSnapshot(); }); diff --git a/src/index.js b/src/index.js index 840f0a0..56139b2 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ const prettyFormat = require('pretty-format'); const { ReactElement } = prettyFormat.plugins; const reactElement = Symbol.for('react.element'); +const MULTILINE_REGEXP = /[\r\n]/; type Options = { expand?: boolean, @@ -50,6 +51,15 @@ const isReactComponent = (value: any) => value && value.$$typeof === reactElement; function diffStrings(valueA: any, valueB: any, options: Options) { + const multiline = + MULTILINE_REGEXP.test(valueA) && MULTILINE_REGEXP.test(valueB); + + // jest-diff doesn't support single-line values, but we want to + if (typeof valueA === 'string' && typeof valueB === 'string' && !multiline) { + valueA += '\n'; // eslint-disable-line no-param-reassign + valueB += '\n'; // eslint-disable-line no-param-reassign + } + return diff(valueA, valueB, { expand: options.expand, contextLines: options.contextLines, From 0a3fda02b19459fd91778b2c046fce5e89c7d579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 31 Jan 2018 09:56:40 +0100 Subject: [PATCH 3/3] address feedback --- .../__snapshots__/snapshotDiff.test.js.snap | 20 +++++++++++++++++++ __tests__/snapshotDiff.test.js | 2 ++ src/index.js | 7 +++---- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/__tests__/__snapshots__/snapshotDiff.test.js.snap b/__tests__/__snapshots__/snapshotDiff.test.js.snap index 6a068af..db858fa 100644 --- a/__tests__/__snapshots__/snapshotDiff.test.js.snap +++ b/__tests__/__snapshots__/snapshotDiff.test.js.snap @@ -144,3 +144,23 @@ exports[`supports diffing single-line strings 1`] = ` + bar " `; + +exports[`supports diffing single-line strings 2`] = ` +"Snapshot Diff: +- First value ++ Second value + +- foo ++ bar + " +`; + +exports[`supports diffing single-line strings 3`] = ` +"Snapshot Diff: +- First value ++ Second value + +- foo ++ bar + " +`; diff --git a/__tests__/snapshotDiff.test.js b/__tests__/snapshotDiff.test.js index a7a185d..c64c8b0 100644 --- a/__tests__/snapshotDiff.test.js +++ b/__tests__/snapshotDiff.test.js @@ -58,6 +58,8 @@ class Component extends React.Component { test('supports diffing single-line strings', () => { expect(snapshotDiff('foo', 'bar')).toMatchSnapshot(); + expect(snapshotDiff('foo\n', 'bar')).toMatchSnapshot(); + expect(snapshotDiff('foo', 'bar\n')).toMatchSnapshot(); }); test('collapses diffs and strips ansi by default', () => { diff --git a/src/index.js b/src/index.js index 56139b2..fa4503b 100644 --- a/src/index.js +++ b/src/index.js @@ -51,12 +51,11 @@ const isReactComponent = (value: any) => value && value.$$typeof === reactElement; function diffStrings(valueA: any, valueB: any, options: Options) { - const multiline = - MULTILINE_REGEXP.test(valueA) && MULTILINE_REGEXP.test(valueB); - // jest-diff doesn't support single-line values, but we want to - if (typeof valueA === 'string' && typeof valueB === 'string' && !multiline) { + if (typeof valueA === 'string' && !MULTILINE_REGEXP.test(valueA)) { valueA += '\n'; // eslint-disable-line no-param-reassign + } + if (typeof valueB === 'string' && !MULTILINE_REGEXP.test(valueB)) { valueB += '\n'; // eslint-disable-line no-param-reassign }