diff --git a/.eslintrc.js b/.eslintrc.js index 86ad482eff..dec7cd8f30 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -635,6 +635,7 @@ module.exports = { FocusOptions: 'readonly', OptionalEffectTiming: 'readonly', + __REACT_ROOT_PATH_TEST__: 'readonly', spyOnDev: 'readonly', spyOnDevAndProd: 'readonly', spyOnProd: 'readonly', diff --git a/packages/internal-test-utils/debugInfo.js b/packages/internal-test-utils/debugInfo.js index 75183ce4b6..b749c8db86 100644 --- a/packages/internal-test-utils/debugInfo.js +++ b/packages/internal-test-utils/debugInfo.js @@ -1,9 +1,5 @@ 'use strict'; -const path = require('path'); - -const repoRoot = path.resolve(__dirname, '../../'); - type DebugInfoConfig = { ignoreProps?: boolean, ignoreRscStreamInfo?: boolean, @@ -34,7 +30,7 @@ function normalizeStack(stack) { const [name, file, line, col, enclosingLine, enclosingCol] = stack[i]; copy.push([ name, - file.replace(repoRoot, ''), + file.replace(__REACT_ROOT_PATH_TEST__, ''), line, col, enclosingLine, diff --git a/packages/react-client/src/__tests__/ReactFlight-test.js b/packages/react-client/src/__tests__/ReactFlight-test.js index d642a02c8c..b2dcd69899 100644 --- a/packages/react-client/src/__tests__/ReactFlight-test.js +++ b/packages/react-client/src/__tests__/ReactFlight-test.js @@ -10,8 +10,6 @@ 'use strict'; -const path = require('path'); - if (typeof Blob === 'undefined') { global.Blob = require('buffer').Blob; } @@ -33,9 +31,8 @@ function normalizeCodeLocInfo(str) { ); } -const repoRoot = path.resolve(__dirname, '../../../../'); function normalizeReactCodeLocInfo(str) { - const repoRootForRegexp = repoRoot.replace(/\//g, '\\/'); + const repoRootForRegexp = __REACT_ROOT_PATH_TEST__.replace(/\//g, '\\/'); const repoFileLocMatch = new RegExp(`${repoRootForRegexp}.+?:\\d+:\\d+`, 'g'); return str && str.replace(repoFileLocMatch, '**'); } diff --git a/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js b/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js index a7290f9523..995f0da37e 100644 --- a/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js +++ b/packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js @@ -39,6 +39,10 @@ function normalizeCodeLocInfo(str) { ); } +function normalizeSerializedContent(str) { + return str.replaceAll(__REACT_ROOT_PATH_TEST__, '**'); +} + describe('ReactFlightDOMEdge', () => { beforeEach(() => { // Mock performance.now for timing tests @@ -481,8 +485,10 @@ describe('ReactFlightDOMEdge', () => { ); const [stream1, stream2] = passThrough(stream).tee(); - const serializedContent = await readResult(stream1); - expect(serializedContent.length).toBeLessThan(1100); + const serializedContent = normalizeSerializedContent( + await readResult(stream1), + ); + expect(serializedContent.length).toBeLessThan(1075); const result = await ReactServerDOMClient.createFromReadableStream( stream2, @@ -551,9 +557,11 @@ describe('ReactFlightDOMEdge', () => { ); const [stream1, stream2] = passThrough(stream).tee(); - const serializedContent = await readResult(stream1); + const serializedContent = normalizeSerializedContent( + await readResult(stream1), + ); - expect(serializedContent.length).toBeLessThan(490); + expect(serializedContent.length).toBeLessThan(465); expect(timesRendered).toBeLessThan(5); const model = await ReactServerDOMClient.createFromReadableStream(stream2, { @@ -623,8 +631,10 @@ describe('ReactFlightDOMEdge', () => { ); const [stream1, stream2] = passThrough(stream).tee(); - const serializedContent = await readResult(stream1); - expect(serializedContent.length).toBeLessThan(__DEV__ ? 680 : 400); + const serializedContent = normalizeSerializedContent( + await readResult(stream1), + ); + expect(serializedContent.length).toBeLessThan(__DEV__ ? 630 : 400); expect(timesRendered).toBeLessThan(5); const model = await serverAct(() => @@ -657,8 +667,10 @@ describe('ReactFlightDOMEdge', () => { , ), ); - const serializedContent = await readResult(stream); - const expectedDebugInfoSize = __DEV__ ? 320 * 20 : 0; + const serializedContent = normalizeSerializedContent( + await readResult(stream), + ); + const expectedDebugInfoSize = __DEV__ ? 295 * 20 : 0; expect(serializedContent.length).toBeLessThan(150 + expectedDebugInfoSize); }); diff --git a/scripts/jest/setupTests.js b/scripts/jest/setupTests.js index 758ac5e0a0..c3d3408906 100644 --- a/scripts/jest/setupTests.js +++ b/scripts/jest/setupTests.js @@ -6,6 +6,7 @@ const { resetAllUnexpectedConsoleCalls, patchConsoleMethods, } = require('internal-test-utils/consoleMock'); +const path = require('path'); if (process.env.REACT_CLASS_EQUIVALENCE_TEST) { // Inside the class equivalence tester, we have a custom environment, let's @@ -18,6 +19,9 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) { const spyOn = jest.spyOn; const noop = jest.fn; + // Can be used to normalize paths in stackframes + global.__REACT_ROOT_PATH_TEST__ = path.resolve(__dirname, '../..'); + // Spying on console methods in production builds can mask errors. // This is why we added an explicit spyOnDev() helper. // It's too easy to accidentally use the more familiar spyOn() helper though,