Skip to content

Commit

Permalink
Normalize React source paths
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jul 18, 2024
1 parent 7a8d371 commit dc8580f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

'use strict';

const path = require('path');

if (typeof Blob === 'undefined') {
global.Blob = require('buffer').Blob;
}
Expand All @@ -27,15 +29,23 @@ function normalizeCodeLocInfo(str) {
);
}

const repoRoot = path.resolve(__dirname, '../../../../');
function normalizeReactCodeLocInfo(str) {
const repoRootForRegexp = repoRoot.replace(/\//g, '\\/');
const repoFileLocMatch = new RegExp(`${repoRootForRegexp}.+?:\\d+:\\d+`, 'g');
return str && str.replace(repoFileLocMatch, '**');
}

// If we just use the original Error prototype, Jest will only display the error message if assertions fail.
// But we usually want to also assert on expando properties or even the stack.
// By hiding the fact from Jest that this is an error, it will show all enumerable properties on mismatch.

function getErrorForJestMatcher(error) {
return {
...error,
// non-enumerable properties that are still relevant for testing
message: error.message,
stack: error.stack,
stack: normalizeReactCodeLocInfo(error.stack),
};
}

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

0 comments on commit dc8580f

Please sign in to comment.