1- import type { ElementType } from 'react' ;
21import type { ReactTestInstance } from 'react-test-renderer' ;
32import {
43 EXPECTED_COLOR ,
@@ -8,10 +7,8 @@ import {
87 RECEIVED_COLOR ,
98 stringify ,
109} from 'jest-matcher-utils' ;
11- import prettyFormat , { plugins } from 'pretty-format' ;
1210import redent from 'redent' ;
1311import { isHostElement } from '../helpers/component-tree' ;
14- import { defaultMapProps } from '../helpers/map-props' ;
1512
1613class HostElementTypeError extends Error {
1714 constructor ( received : unknown , matcherFn : jest . CustomMatcher , context : jest . MatcherContext ) {
@@ -61,81 +58,6 @@ export type FormatElementOptions = {
6158 minimal ?: boolean ;
6259} ;
6360
64- /***
65- * Format given element as a pretty-printed string.
66- *
67- * @param element Element to format.
68- */
69- export function formatElement (
70- element : ReactTestInstance | null ,
71- { minimal = false } : FormatElementOptions = { } ,
72- ) {
73- if ( element == null ) {
74- return ' null' ;
75- }
76-
77- const { children, ...props } = element . props ;
78- const childrenToDisplay = typeof children === 'string' ? [ children ] : undefined ;
79-
80- return redent (
81- prettyFormat (
82- {
83- // This prop is needed persuade the prettyFormat that the element is
84- // a ReactTestRendererJSON instance, so it is formatted as JSX.
85- $$typeof : Symbol . for ( 'react.test.json' ) ,
86- type : formatElementType ( element . type ) ,
87- props : defaultMapProps ( props ) ,
88- children : childrenToDisplay ,
89- } ,
90- // See: https://www.npmjs.com/package/pretty-format#usage-with-options
91- {
92- plugins : [ plugins . ReactTestComponent , plugins . ReactElement ] ,
93- printFunctionName : false ,
94- printBasicPrototype : false ,
95- highlight : true ,
96- min : minimal ,
97- } ,
98- ) ,
99- 2 ,
100- ) ;
101- }
102-
103- export function formatElementType ( type : ElementType ) : string {
104- if ( typeof type === 'function' ) {
105- return type . displayName ?? type . name ;
106- }
107-
108- // if (typeof type === 'object') {
109- // console.log('OBJECT', type);
110- // }
111-
112- if ( typeof type === 'object' && 'type' in type ) {
113- // @ts -expect-error: despite typing this can happen
114- const nestedType = formatElementType ( type . type ) ;
115- if ( nestedType ) {
116- return nestedType ;
117- }
118- }
119-
120- if ( typeof type === 'object' && 'render' in type ) {
121- // @ts -expect-error: despite typing this can happen
122- const nestedType = formatElementType ( type . render ) ;
123- if ( nestedType ) {
124- return nestedType ;
125- }
126- }
127-
128- return `${ type } ` ;
129- }
130-
131- export function formatElementArray ( elements : ReactTestInstance [ ] , options ?: FormatElementOptions ) {
132- if ( elements . length === 0 ) {
133- return ' (no elements)' ;
134- }
135-
136- return redent ( elements . map ( ( element ) => formatElement ( element , options ) ) . join ( '\n' ) , 2 ) ;
137- }
138-
13961export function formatMessage (
14062 matcher : string ,
14163 expectedLabel : string ,
0 commit comments