10
10
11
11
'use strict' ;
12
12
13
+ const path = require ( 'path' ) ;
14
+
13
15
if ( typeof Blob === 'undefined' ) {
14
16
global . Blob = require ( 'buffer' ) . Blob ;
15
17
}
@@ -41,15 +43,23 @@ function formatV8Stack(stack) {
41
43
return v8StyleStack ;
42
44
}
43
45
46
+ const repoRoot = path . resolve ( __dirname , '../../../../' ) ;
47
+ function normalizeReactCodeLocInfo ( str ) {
48
+ const repoRootForRegexp = repoRoot . replace ( / \/ / g, '\\/' ) ;
49
+ const repoFileLocMatch = new RegExp ( `${ repoRootForRegexp } .+?:\\d+:\\d+` , 'g' ) ;
50
+ return str && str . replace ( repoFileLocMatch , '**' ) ;
51
+ }
52
+
44
53
// If we just use the original Error prototype, Jest will only display the error message if assertions fail.
45
54
// But we usually want to also assert on our expando properties or even the stack.
46
55
// By hiding the fact from Jest that this is an error, it will show all enumerable properties on mismatch.
56
+
47
57
function getErrorForJestMatcher ( error ) {
48
58
return {
49
59
...error ,
50
60
// non-enumerable properties that are still relevant for testing
51
61
message : error . message ,
52
- stack : error . stack ,
62
+ stack : normalizeReactCodeLocInfo ( error . stack ) ,
53
63
} ;
54
64
}
55
65
@@ -1211,7 +1221,7 @@ describe('ReactFlight', () => {
1211
1221
throw error ;
1212
1222
}
1213
1223
1214
- const findSourceMapURL = jest . fn ( ) ;
1224
+ const findSourceMapURL = jest . fn ( ( ) => null ) ;
1215
1225
const errors = [ ] ;
1216
1226
class MyErrorBoundary extends React . Component {
1217
1227
state = { error : null } ;
@@ -1273,11 +1283,15 @@ describe('ReactFlight', () => {
1273
1283
stack : gate ( flags => flags . enableOwnerStacks )
1274
1284
? expect . stringContaining (
1275
1285
'Error: This is an error\n' +
1286
+ ' at eval (eval at testFunction (eval at createFakeFunction (**), <anonymous>:1:35)\n' +
1287
+ ' at ServerComponentError (file://~/(some)(really)(exotic-directory)/ReactFlight-test.js:1166:19)\n' +
1276
1288
' at (anonymous) (file:///testing.js:42:3)\n' +
1277
1289
' at (anonymous) (file:///testing.js:42:3)\n' ,
1278
1290
)
1279
1291
: expect . stringContaining (
1280
1292
'Error: This is an error\n' +
1293
+ ' at eval (eval at testFunction (inspected-page.html:29:11), <anonymous>:1:10)\n' +
1294
+ ' at ServerComponentError (file://~/(some)(really)(exotic-directory)/ReactFlight-test.js:1166:19)\n' +
1281
1295
' at file:///testing.js:42:3\n' +
1282
1296
' at file:///testing.js:42:3' ,
1283
1297
) ,
@@ -1286,7 +1300,15 @@ describe('ReactFlight', () => {
1286
1300
} ,
1287
1301
] ,
1288
1302
findSourceMapURLCalls : gate ( flags => flags . enableOwnerStacks )
1289
- ? [ [ __filename ] , [ __filename ] , [ 'file:///testing.js' ] , [ __filename ] ]
1303
+ ? [
1304
+ [ __filename ] ,
1305
+ [ __filename ] ,
1306
+ // TODO: What should we request here? The outer (<anonymous>) or the inner (inspected-page.html)?
1307
+ [ 'inspected-page.html:29:11), <anonymous>' ] ,
1308
+ [ 'file://~/(some)(really)(exotic-directory)/ReactFlight-test.js' ] ,
1309
+ [ 'file:///testing.js' ] ,
1310
+ [ __filename ] ,
1311
+ ]
1290
1312
: [ ] ,
1291
1313
} ) ;
1292
1314
} else {
0 commit comments