|
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}; |
@@ -1270,19 +1280,34 @@ describe('ReactFlight', () => { |
1270 | 1280 | errors: [ |
1271 | 1281 | { |
1272 | 1282 | message: 'This is an error', |
1273 | | - stack: expect.stringContaining( |
1274 | | - 'Error: This is an error\n' + |
1275 | | - ' at (anonymous) (file:///testing.js:42:3)\n' + |
1276 | | - ' at (anonymous) (file:///testing.js:42:3)\n', |
1277 | | - ), |
| 1283 | + stack: gate(flags => flags.enableOwnerStacks) |
| 1284 | + ? expect.stringContaining( |
| 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' + |
| 1288 | + ' at (anonymous) (file:///testing.js:42:3)\n' + |
| 1289 | + ' at (anonymous) (file:///testing.js:42:3)\n', |
| 1290 | + ) |
| 1291 | + : expect.stringContaining( |
| 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' + |
| 1295 | + ' at file:///testing.js:42:3\n' + |
| 1296 | + ' at async file:///testing.js:42:3\n', |
| 1297 | + ), |
1278 | 1298 | digest: 'a dev digest', |
1279 | 1299 | environmentName: 'Server', |
1280 | 1300 | }, |
1281 | 1301 | ], |
1282 | | - findSourceMapURLCalls: expect.arrayContaining([ |
1283 | | - ['file:///testing.js'], |
1284 | | - ['file:///testing.js'], |
1285 | | - ]), |
| 1302 | + findSourceMapURLCalls: gate(flags => flags.enableOwnerStacks) |
| 1303 | + ? expect.arrayContaining([ |
| 1304 | + // TODO: What should we request here? The outer (<anonymous>) or the inner (inspected-page.html)? |
| 1305 | + ['inspected-page.html:29:11), <anonymous>'], |
| 1306 | + ['file://~/(some)(really)(exotic-directory)/ReactFlight-test.js'], |
| 1307 | + ['file:///testing.js'], |
| 1308 | + ['file:///testing.js'], |
| 1309 | + ]) |
| 1310 | + : [], |
1286 | 1311 | }); |
1287 | 1312 | } else { |
1288 | 1313 | expect(errors.map(getErrorForJestMatcher)).toEqual([ |
|
0 commit comments