10
10
11
11
'use strict' ;
12
12
13
+ let act ;
13
14
let React ;
14
15
let ReactNoopServer ;
15
16
17
+ function normalizeCodeLocInfo ( str ) {
18
+ return (
19
+ str &&
20
+ str . replace ( / ^ + (?: a t | i n ) ( [ \S ] + ) [ ^ \n ] * / gm, function ( m , name ) {
21
+ const dot = name . lastIndexOf ( '.' ) ;
22
+ if ( dot !== - 1 ) {
23
+ name = name . slice ( dot + 1 ) ;
24
+ }
25
+ return ' in ' + name + ( / \d / . test ( m ) ? ' (at **)' : '' ) ;
26
+ } )
27
+ ) ;
28
+ }
29
+
16
30
describe ( 'ReactServer' , ( ) => {
17
31
beforeEach ( ( ) => {
18
32
jest . resetModules ( ) ;
19
33
34
+ act = require ( 'internal-test-utils' ) . act ;
20
35
React = require ( 'react' ) ;
21
36
ReactNoopServer = require ( 'react-noop-renderer/server' ) ;
22
37
} ) ;
@@ -32,4 +47,42 @@ describe('ReactServer', () => {
32
47
const result = ReactNoopServer . render ( < div > hello world</ div > ) ;
33
48
expect ( result . root ) . toEqual ( div ( 'hello world' ) ) ;
34
49
} ) ;
50
+
51
+ it ( 'has Owner Stacks when aborted' , async ( ) => {
52
+ function Component ( { promise} ) {
53
+ React . use ( promise ) ;
54
+ return < div > Hello, Dave!</ div > ;
55
+ }
56
+ function App ( { promise} ) {
57
+ return < Component promise = { promise } /> ;
58
+ }
59
+
60
+ let caughtError ;
61
+ let componentStack ;
62
+ let ownerStack ;
63
+ const result = ReactNoopServer . render (
64
+ < App promise = { new Promise ( ( ) => { } ) } /> ,
65
+ {
66
+ onError : ( error , errorInfo ) => {
67
+ caughtError = error ;
68
+ componentStack = errorInfo . componentStack ;
69
+ ownerStack = __DEV__ ? React . captureOwnerStack ( ) : null ;
70
+ } ,
71
+ } ,
72
+ ) ;
73
+
74
+ await act ( async ( ) => {
75
+ result . abort ( ) ;
76
+ } ) ;
77
+ expect ( caughtError ) . toEqual (
78
+ expect . objectContaining ( {
79
+ message : 'The render was aborted by the server without a reason.' ,
80
+ } ) ,
81
+ ) ;
82
+ expect ( normalizeCodeLocInfo ( componentStack ) ) . toEqual (
83
+ '\n in Component (at **)' + '\n in App (at **)' ,
84
+ ) ;
85
+ // FIXME: Should have a stack.
86
+ expect ( normalizeCodeLocInfo ( ownerStack ) ) . toEqual ( null ) ;
87
+ } ) ;
35
88
} ) ;
0 commit comments