diff --git a/__tests__/__snapshots__/snapshotDiff.test.js.snap b/__tests__/__snapshots__/snapshotDiff.test.js.snap index 8143537..299d5ab 100644 --- a/__tests__/__snapshots__/snapshotDiff.test.js.snap +++ b/__tests__/__snapshots__/snapshotDiff.test.js.snap @@ -50,16 +50,16 @@ exports[`can use contextLines on diff 1`] = ` exports[`can use contextLines with React components 1`] = ` "Snapshot Diff: -- -+ +- ++ -@@ -6,1 +6,1 @@ +@@ -10,1 +10,1 @@ - say + my name -@@ -9,1 +9,1 @@ +@@ -13,1 +13,1 @@ - say + my name -@@ -32,1 +32,1 @@ +@@ -36,1 +36,1 @@ - say + my name" `; @@ -115,15 +115,15 @@ exports[`collapses diffs and strips ansi by default 1`] = ` exports[`detects React components 1`] = ` "Snapshot Diff: -- -+ - -@@ -1,14 +1,14 @@ -
- - - - +- ++ + +@@ -5,14 +5,14 @@ + - say + my name @@ -135,7 +135,7 @@ exports[`detects React components 1`] = ` -@@ -27,11 +27,11 @@ +@@ -31,11 +31,11 @@ @@ -165,3 +165,21 @@ 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[`shows diff when comparing React fragments of varying length 1`] = ` +"Snapshot Diff: +- ++ + +-
+- First +-
++ Array [ ++
++ First ++
, ++
++ Second ++
, ++ ]" +`; diff --git a/__tests__/snapshotDiff.test.js b/__tests__/snapshotDiff.test.js index 5ab0c04..0d31a3f 100644 --- a/__tests__/snapshotDiff.test.js +++ b/__tests__/snapshotDiff.test.js @@ -1,5 +1,7 @@ // @flow +/* eslint-disable react/no-multi-comp */ + const React = require('react'); const snapshotDiff = require('../src/index'); @@ -87,7 +89,13 @@ class Component extends React.Component { - {this.props.test} + + {this.props.test} + {this.props.test} @@ -159,6 +167,23 @@ class Component extends React.Component { } } +class FragmentComponent extends React.Component { + render() { + return ( + <> +
First
+ {this.props.withSecond ?
Second
: null} + + ); + } +} + +const props = { + unnamedFunction: () => {}, + unnamedJestMock: jest.fn(), + namedJestMock: jest.fn().mockName('test-mock-name'), +}; + test('collapses diffs and strips ansi by default', () => { expect(snapshotDiff(a, b)).toMatchSnapshot(); }); @@ -187,15 +212,28 @@ test('diffs short strings', () => { test('detects React components', () => { expect( - snapshotDiff(, ) + snapshotDiff( + , + + ) ).toMatchSnapshot(); }); test('can use contextLines with React components', () => { expect( - snapshotDiff(, , { - contextLines: 0, - }) + snapshotDiff( + , + , + { + contextLines: 0, + } + ) + ).toMatchSnapshot(); +}); + +test('shows diff when comparing React fragments of varying length', () => { + expect( + snapshotDiff(, ) ).toMatchSnapshot(); }); diff --git a/src/react-serializer.js b/src/react-serializer.js index 3f4dd18..c49ac88 100644 --- a/src/react-serializer.js +++ b/src/react-serializer.js @@ -3,8 +3,10 @@ 'use strict'; const prettyFormat = require('pretty-format'); +const snapshot = require('jest-snapshot'); + +const serializers = snapshot.getSerializers(); -const { ReactElement } = prettyFormat.plugins; const reactElement = Symbol.for('react.element'); function getReactComponentSerializer() { @@ -22,7 +24,8 @@ function getReactComponentSerializer() { } throw error; } - return value => renderer.create(value).toJSON(); + return value => + prettyFormat(renderer.create(value), { plugins: serializers }); } const reactSerializer = { @@ -32,7 +35,7 @@ const reactSerializer = { return reactComponentSerializer(value); }, diffOptions: (valueA: any, valueB: any) => { - const prettyFormatOptions = { plugins: [ReactElement], min: true }; + const prettyFormatOptions = { plugins: serializers, min: true }; return { aAnnotation: prettyFormat(valueA, prettyFormatOptions), bAnnotation: prettyFormat(valueB, prettyFormatOptions),