@@ -31,26 +31,48 @@ export function formatElement(
3131 const { children, ...props } = element . props ;
3232 const childrenToDisplay = typeof children === 'string' ? [ children ] : undefined ;
3333
34- return prettyFormat (
35- {
36- // This prop is needed persuade the prettyFormat that the element is
37- // a ReactTestRendererJSON instance, so it is formatted as JSX.
38- $$typeof : Symbol . for ( 'react.test.json' ) ,
39- type : `${ element . type } ` ,
40- props : mapProps ? mapProps ( props ) : props ,
41- children : childrenToDisplay ,
42- } ,
43- // See: https://www.npmjs.com/package/pretty-format#usage-with-options
44- {
45- plugins : [ plugins . ReactTestComponent , plugins . ReactElement ] ,
46- printFunctionName : false ,
47- printBasicPrototype : false ,
48- highlight : highlight ,
49- min : compact ,
50- } ,
34+ return (
35+ ( typeof element . type === 'string' ? '' : 'composite ' ) +
36+ prettyFormat (
37+ {
38+ // This prop is needed persuade the prettyFormat that the element is
39+ // a ReactTestRendererJSON instance, so it is formatted as JSX.
40+ $$typeof : Symbol . for ( 'react.test.json' ) ,
41+ type : formatElementName ( element . type ) ,
42+ props : mapProps ? mapProps ( props ) : props ,
43+ children : childrenToDisplay ,
44+ } ,
45+ // See: https://www.npmjs.com/package/pretty-format#usage-with-options
46+ {
47+ plugins : [ plugins . ReactTestComponent , plugins . ReactElement ] ,
48+ printFunctionName : false ,
49+ printBasicPrototype : false ,
50+ highlight : highlight ,
51+ min : compact ,
52+ } ,
53+ )
5154 ) ;
5255}
5356
57+ function formatElementName ( type : ReactTestInstance [ 'type' ] ) {
58+ if ( typeof type === 'function' ) {
59+ return type . displayName ?? type . name ;
60+ }
61+
62+ if ( typeof type === 'object' ) {
63+ if ( 'type' in type ) {
64+ // @ts -expect-error: despite typing this can happen for React.memo.
65+ return formatElementName ( type . type ) ;
66+ }
67+ if ( 'render' in type ) {
68+ // @ts -expect-error: despite typing this can happen for React.forwardRefs.
69+ return formatElementName ( type . render ) ;
70+ }
71+ }
72+
73+ return `${ type } ` ;
74+ }
75+
5476export function formatElementList ( elements : ReactTestInstance [ ] , options ?: FormatElementOptions ) {
5577 if ( elements . length === 0 ) {
5678 return '(no elements)' ;
0 commit comments