diff --git a/__tests__/__snapshots__/snapshotDiff.test.js.snap b/__tests__/__snapshots__/snapshotDiff.test.js.snap index b3b9d94..db858fa 100644 --- a/__tests__/__snapshots__/snapshotDiff.test.js.snap +++ b/__tests__/__snapshots__/snapshotDiff.test.js.snap @@ -134,3 +134,33 @@ 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 + " +`; + +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 29ce66b..c64c8b0 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,76 +48,20 @@ class Component extends React.Component { {this.props.test} {this.props.test} - - - - - - - - - - - - - - - - - - - - + {dummySpans} {this.props.test} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + {dummySpans}
); } } +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', () => { expect(snapshotDiff(a, b)).toMatchSnapshot(); }); diff --git a/src/index.js b/src/index.js index 840f0a0..fa4503b 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,14 @@ const isReactComponent = (value: any) => value && value.$$typeof === reactElement; function diffStrings(valueA: any, valueB: any, options: Options) { + // jest-diff doesn't support single-line values, but we want to + 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 + } + return diff(valueA, valueB, { expand: options.expand, contextLines: options.contextLines,