@@ -33,18 +33,39 @@ export const render = (
33
33
const renderer = TestRenderer . create ( component , options ) ;
34
34
const instance = renderer . root ;
35
35
36
+ const getByName = ( name : string | React . Element < * > ) => {
37
+ try {
38
+ return instance . find ( node => getNodeByName ( node , name ) ) ;
39
+ } catch ( error ) {
40
+ throw new ErrorWithStack ( `Error: Component not found.` , getByName ) ;
41
+ }
42
+ } ;
43
+
44
+ const getByText = ( text : string | RegExp ) => {
45
+ try {
46
+ return instance . find ( node => getNodeByText ( node , text ) ) ;
47
+ } catch ( error ) {
48
+ throw new ErrorWithStack ( `Error: Component not found.` , getByText ) ;
49
+ }
50
+ } ;
51
+
52
+ const getByProps = ( props : { [ propName : string ] : any } ) => {
53
+ try {
54
+ return instance . findByProps ( props ) ;
55
+ } catch ( error ) {
56
+ throw new ErrorWithStack ( `Error: Component not found.` , getByProps ) ;
57
+ }
58
+ } ;
59
+
36
60
return {
37
61
getByTestId : ( testID : string ) => instance . findByProps ( { testID } ) ,
38
- getByName : ( name : string | React . Element < * > ) =>
39
- instance . find ( node => getNodeByName ( node , name ) ) ,
62
+ getByName ,
40
63
getAllByName : ( name : string | React . Element < * > ) =>
41
64
instance . findAll ( node => getNodeByName ( node , name ) ) ,
42
- getByText : ( text : string | RegExp ) =>
43
- instance . find ( node => getNodeByText ( node , text ) ) ,
65
+ getByText ,
44
66
getAllByText : ( text : string | RegExp ) = >
45
67
instance . findAll ( node => getNodeByText ( node , text ) ) ,
46
- getByProps : ( props : { [ propName : string ] : any } ) =>
47
- instance . findByProps ( props ) ,
68
+ getByProps ,
48
69
getAllByProps : ( props : { [ propName : string ] : any } ) = >
49
70
instance . findAllByProps ( props ) ,
50
71
update : renderer . update ,
@@ -79,10 +100,19 @@ export const debug = (
79
100
) => {
80
101
const { output } = shallow ( instance ) ;
81
102
// eslint-disable-next-line no-console
82
- console . log (
83
- prettyFormat ( output , {
84
- plugins : [ plugins . ReactTestComponent , plugins . ReactElement ] ,
85
- } ) ,
86
- message || ''
87
- ) ;
103
+ console . log ( format ( output ) , message || '' ) ;
88
104
} ;
105
+
106
+ const format = input =>
107
+ prettyFormat ( input , {
108
+ plugins : [ plugins . ReactTestComponent , plugins . ReactElement ] ,
109
+ } ) ;
110
+
111
+ class ErrorWithStack extends Error {
112
+ constructor ( message : ?string , callsite : Function ) {
113
+ super ( message ) ;
114
+ if ( Error . captureStackTrace ) {
115
+ Error . captureStackTrace ( this , callsite ) ;
116
+ }
117
+ }
118
+ }
0 commit comments