Skip to content

Commit dc8580f

Browse files
committed
Normalize React source paths
1 parent 7a8d371 commit dc8580f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
'use strict';
1212

13+
const path = require('path');
14+
1315
if (typeof Blob === 'undefined') {
1416
global.Blob = require('buffer').Blob;
1517
}
@@ -27,15 +29,23 @@ function normalizeCodeLocInfo(str) {
2729
);
2830
}
2931

32+
const repoRoot = path.resolve(__dirname, '../../../../');
33+
function normalizeReactCodeLocInfo(str) {
34+
const repoRootForRegexp = repoRoot.replace(/\//g, '\\/');
35+
const repoFileLocMatch = new RegExp(`${repoRootForRegexp}.+?:\\d+:\\d+`, 'g');
36+
return str && str.replace(repoFileLocMatch, '**');
37+
}
38+
3039
// If we just use the original Error prototype, Jest will only display the error message if assertions fail.
3140
// But we usually want to also assert on expando properties or even the stack.
3241
// By hiding the fact from Jest that this is an error, it will show all enumerable properties on mismatch.
42+
3343
function getErrorForJestMatcher(error) {
3444
return {
3545
...error,
3646
// non-enumerable properties that are still relevant for testing
3747
message: error.message,
38-
stack: error.stack,
48+
stack: normalizeReactCodeLocInfo(error.stack),
3949
};
4050
}
4151

@@ -1251,7 +1261,7 @@ describe('ReactFlight', () => {
12511261
message: 'This is an error',
12521262
stack: expect.stringContaining(
12531263
'Error: This is an error\n' +
1254-
' at eval (eval at testFunction (eval at createFakeFunction (/Users/sebbie/repos/react/packages/react-client/src/ReactFlightClient.js:1948:19), <anonymous>:1:35)\n' +
1264+
' at eval (eval at testFunction (eval at createFakeFunction (**), <anonymous>:1:35)\n' +
12551265
' at ServerComponentError (file://~/(some)(really)(exotic-directory)/ReactFlight-test.js:1166:19)\n' +
12561266
' at (anonymous) (file:///testing.js:42:3)\n' +
12571267
' at (anonymous) (file:///testing.js:42:3)\n',

0 commit comments

Comments
 (0)