44
55const diff = require ( 'jest-diff' ) ;
66const snapshot = require ( 'jest-snapshot' ) ;
7- const prettyFormat = require ( 'pretty-format' ) ;
8-
9- const { ReactElement } = prettyFormat . plugins ;
10- const reactElement = Symbol . for ( 'react.element' ) ;
7+ const reactSerializer = require ( './react-serializer' ) ;
118
129type Options = { |
1310 expand ? : boolean ,
@@ -29,12 +26,27 @@ const defaultOptions = {
2926
3027const SNAPSHOT_TITLE = 'Snapshot Diff:\n' ;
3128
29+ const identity = value => value ;
30+ const defaultSerializers = [ reactSerializer ] ;
31+ let serializers = defaultSerializers ;
32+
3233const snapshotDiff = ( valueA : any , valueB : any , options ? : Options ) : string => {
3334 let difference ;
3435 const mergedOptions = { ...defaultOptions , ...options } ;
3536
36- if ( isReactComponent ( valueA ) && isReactComponent ( valueB ) ) {
37- difference = diffReactComponents ( valueA , valueB , mergedOptions ) ;
37+ const matchingSerializer = serializers . find (
38+ ( { test } ) => test ( valueA ) && test ( valueB )
39+ ) ;
40+
41+ if ( matchingSerializer ) {
42+ const { print, diffOptions } = matchingSerializer ;
43+ const serializerOptions = diffOptions
44+ ? diffOptions ( valueA , valueB ) || { }
45+ : { } ;
46+ difference = diffStrings ( print ( valueA , identity ) , print ( valueB , identity ) , {
47+ ...mergedOptions ,
48+ ...serializerOptions ,
49+ } ) ;
3850 } else {
3951 difference = diffStrings ( valueA , valueB , mergedOptions ) ;
4052 }
@@ -55,9 +67,6 @@ const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
5567 return SNAPSHOT_TITLE + difference ;
5668} ;
5769
58- const isReactComponent = ( value : any ) =>
59- value && value . $$typeof === reactElement ;
60-
6170function diffStrings ( valueA : any , valueB : any , options : Options ) {
6271 return diff ( valueA , valueB , {
6372 expand : options . expand ,
@@ -67,36 +76,6 @@ function diffStrings(valueA: any, valueB: any, options: Options) {
6776 } ) ;
6877}
6978
70- function requireReactTestRenderer ( ) {
71- try {
72- return require ( 'react-test-renderer' ) ; // eslint-disable-line import/no-extraneous-dependencies
73- } catch ( error ) {
74- if ( error . code === 'MODULE_NOT_FOUND' ) {
75- throw new Error (
76- `Failed to load optional module "react-test-renderer". ` +
77- `If you need to compare React elements, please add "react-test-renderer" to your ` +
78- `project's dependencies.\n` +
79- `${ error . message } `
80- ) ;
81- }
82- throw error ;
83- }
84- }
85-
86- function diffReactComponents ( valueA : any , valueB : any , options : Options ) {
87- const renderer = requireReactTestRenderer ( ) ;
88- const reactValueA = renderer . create ( valueA ) . toJSON ( ) ;
89- const reactValueB = renderer . create ( valueB ) . toJSON ( ) ;
90- const prettyFormatOptions = { plugins : [ ReactElement ] , min : true } ;
91-
92- return diff ( reactValueA , reactValueB , {
93- expand : options . expand ,
94- contextLines : options . contextLines ,
95- aAnnotation : prettyFormat ( valueA , prettyFormatOptions ) ,
96- bAnnotation : prettyFormat ( valueB , prettyFormatOptions ) ,
97- } ) ;
98- }
99-
10079function toMatchDiffSnapshot (
10180 valueA : any ,
10281 valueB : any ,
@@ -119,7 +98,13 @@ function getSnapshotDiffSerializer() {
11998 } ;
12099}
121100
101+ function setSerializers ( customSerializers ) {
102+ serializers = customSerializers ;
103+ }
104+
122105module . exports = snapshotDiff ;
123106module . exports . snapshotDiff = snapshotDiff ;
124107module . exports . toMatchDiffSnapshot = toMatchDiffSnapshot ;
125108module . exports . getSnapshotDiffSerializer = getSnapshotDiffSerializer ;
109+ module . exports . setSerializers = setSerializers ;
110+ module . exports . defaultSerializers = defaultSerializers ;
0 commit comments