Skip to content

Commit c03a51d

Browse files
authored
Move getDebugInfo test util function to internal-test-utils (#34523)
In an upstack PR, I need `getDebugInfo` in another test file, so I'm moving it to `internal-test-utils` so it can be shared.
1 parent ad578aa commit c03a51d

File tree

5 files changed

+426
-455
lines changed

5 files changed

+426
-455
lines changed

packages/internal-test-utils/ReactInternalTestUtils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
clearErrors,
1717
createLogAssertion,
1818
} from './consoleMock';
19+
export {getDebugInfo} from './debugInfo';
1920
export {act, serverAct} from './internalAct';
2021
const {assertConsoleLogsCleared} = require('internal-test-utils/consoleMock');
2122

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const repoRoot = path.resolve(__dirname, '../../');
6+
7+
type DebugInfoConfig = {
8+
ignoreProps?: boolean,
9+
ignoreRscStreamInfo?: boolean,
10+
useFixedTime?: boolean,
11+
useV8Stack?: boolean,
12+
};
13+
14+
function formatV8Stack(stack) {
15+
let v8StyleStack = '';
16+
if (stack) {
17+
for (let i = 0; i < stack.length; i++) {
18+
const [name] = stack[i];
19+
if (v8StyleStack !== '') {
20+
v8StyleStack += '\n';
21+
}
22+
v8StyleStack += ' in ' + name + ' (at **)';
23+
}
24+
}
25+
return v8StyleStack;
26+
}
27+
28+
function normalizeStack(stack) {
29+
if (!stack) {
30+
return stack;
31+
}
32+
const copy = [];
33+
for (let i = 0; i < stack.length; i++) {
34+
const [name, file, line, col, enclosingLine, enclosingCol] = stack[i];
35+
copy.push([
36+
name,
37+
file.replace(repoRoot, ''),
38+
line,
39+
col,
40+
enclosingLine,
41+
enclosingCol,
42+
]);
43+
}
44+
return copy;
45+
}
46+
47+
function normalizeIOInfo(config: DebugInfoConfig, ioInfo) {
48+
const {debugTask, debugStack, debugLocation, ...copy} = ioInfo;
49+
if (ioInfo.stack) {
50+
copy.stack = config.useV8Stack
51+
? formatV8Stack(ioInfo.stack)
52+
: normalizeStack(ioInfo.stack);
53+
}
54+
if (ioInfo.owner) {
55+
copy.owner = normalizeDebugInfo(config, ioInfo.owner);
56+
}
57+
if (typeof ioInfo.start === 'number' && config.useFixedTime) {
58+
copy.start = 0;
59+
}
60+
if (typeof ioInfo.end === 'number' && config.useFixedTime) {
61+
copy.end = 0;
62+
}
63+
const promise = ioInfo.value;
64+
if (promise) {
65+
promise.then(); // init
66+
if (promise.status === 'fulfilled') {
67+
if (ioInfo.name === 'RSC stream') {
68+
copy.byteSize = 0;
69+
copy.value = {
70+
value: 'stream',
71+
};
72+
} else {
73+
copy.value = {
74+
value: promise.value,
75+
};
76+
}
77+
} else if (promise.status === 'rejected') {
78+
copy.value = {
79+
reason: promise.reason,
80+
};
81+
} else {
82+
copy.value = {
83+
status: promise.status,
84+
};
85+
}
86+
}
87+
return copy;
88+
}
89+
90+
function normalizeDebugInfo(config: DebugInfoConfig, original) {
91+
const {debugTask, debugStack, debugLocation, ...debugInfo} = original;
92+
if (original.owner) {
93+
debugInfo.owner = normalizeDebugInfo(config, original.owner);
94+
}
95+
if (original.awaited) {
96+
debugInfo.awaited = normalizeIOInfo(config, original.awaited);
97+
}
98+
if (debugInfo.props && config.ignoreProps) {
99+
debugInfo.props = {};
100+
}
101+
if (Array.isArray(debugInfo.stack)) {
102+
debugInfo.stack = config.useV8Stack
103+
? formatV8Stack(debugInfo.stack)
104+
: normalizeStack(debugInfo.stack);
105+
return debugInfo;
106+
} else if (typeof debugInfo.time === 'number' && config.useFixedTime) {
107+
return {...debugInfo, time: 0};
108+
} else {
109+
return debugInfo;
110+
}
111+
}
112+
113+
export function getDebugInfo(config: DebugInfoConfig, obj) {
114+
const debugInfo = obj._debugInfo;
115+
if (debugInfo) {
116+
const copy = [];
117+
for (let i = 0; i < debugInfo.length; i++) {
118+
if (
119+
debugInfo[i].awaited &&
120+
debugInfo[i].awaited.name === 'RSC stream' &&
121+
config.ignoreRscStreamInfo
122+
) {
123+
// Ignore RSC stream I/O info.
124+
} else {
125+
copy.push(normalizeDebugInfo(config, debugInfo[i]));
126+
}
127+
}
128+
return copy;
129+
}
130+
return debugInfo;
131+
}

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

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,6 @@ function normalizeCodeLocInfo(str) {
3333
);
3434
}
3535

36-
function formatV8Stack(stack) {
37-
let v8StyleStack = '';
38-
if (stack) {
39-
for (let i = 0; i < stack.length; i++) {
40-
const [name] = stack[i];
41-
if (v8StyleStack !== '') {
42-
v8StyleStack += '\n';
43-
}
44-
v8StyleStack += ' in ' + name + ' (at **)';
45-
}
46-
}
47-
return v8StyleStack;
48-
}
49-
5036
const repoRoot = path.resolve(__dirname, '../../../../');
5137
function normalizeReactCodeLocInfo(str) {
5238
const repoRootForRegexp = repoRoot.replace(/\//g, '\\/');
@@ -67,35 +53,6 @@ function getErrorForJestMatcher(error) {
6753
};
6854
}
6955

70-
function normalizeComponentInfo(debugInfo) {
71-
if (Array.isArray(debugInfo.stack)) {
72-
const {debugTask, debugStack, debugLocation, ...copy} = debugInfo;
73-
copy.stack = formatV8Stack(debugInfo.stack);
74-
if (debugInfo.owner) {
75-
copy.owner = normalizeComponentInfo(debugInfo.owner);
76-
}
77-
return copy;
78-
} else {
79-
return debugInfo;
80-
}
81-
}
82-
83-
function getDebugInfo(obj) {
84-
const debugInfo = obj._debugInfo;
85-
if (debugInfo) {
86-
const copy = [];
87-
for (let i = 0; i < debugInfo.length; i++) {
88-
if (debugInfo[i].awaited && debugInfo[i].awaited.name === 'RSC stream') {
89-
// Ignore RSC stream I/O info.
90-
} else {
91-
copy.push(normalizeComponentInfo(debugInfo[i]));
92-
}
93-
}
94-
return copy;
95-
}
96-
return debugInfo;
97-
}
98-
9956
const finalizationRegistries = [];
10057
function FinalizationRegistryMock(callback) {
10158
this._heldValues = [];
@@ -132,6 +89,7 @@ let NoErrorExpected;
13289
let Scheduler;
13390
let assertLog;
13491
let assertConsoleErrorDev;
92+
let getDebugInfo;
13593

13694
describe('ReactFlight', () => {
13795
beforeEach(() => {
@@ -169,6 +127,11 @@ describe('ReactFlight', () => {
169127
assertLog = InternalTestUtils.assertLog;
170128
assertConsoleErrorDev = InternalTestUtils.assertConsoleErrorDev;
171129

130+
getDebugInfo = InternalTestUtils.getDebugInfo.bind(null, {
131+
useV8Stack: true,
132+
ignoreRscStreamInfo: true,
133+
});
134+
172135
ErrorBoundary = class extends React.Component {
173136
state = {hasError: false, error: null};
174137
static getDerivedStateFromError(error) {

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

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,12 @@ if (typeof File === 'undefined' || typeof FormData === 'undefined') {
1818
global.FormData = require('undici').FormData;
1919
}
2020

21-
function formatV8Stack(stack) {
22-
let v8StyleStack = '';
23-
if (stack) {
24-
for (let i = 0; i < stack.length; i++) {
25-
const [name] = stack[i];
26-
if (v8StyleStack !== '') {
27-
v8StyleStack += '\n';
28-
}
29-
v8StyleStack += ' in ' + name + ' (at **)';
30-
}
31-
}
32-
return v8StyleStack;
33-
}
34-
35-
function normalizeComponentInfo(debugInfo) {
36-
if (Array.isArray(debugInfo.stack)) {
37-
const {debugTask, debugStack, ...copy} = debugInfo;
38-
copy.stack = formatV8Stack(debugInfo.stack);
39-
if (debugInfo.owner) {
40-
copy.owner = normalizeComponentInfo(debugInfo.owner);
41-
}
42-
return copy;
43-
} else {
44-
return debugInfo;
45-
}
46-
}
47-
48-
function getDebugInfo(obj) {
49-
const debugInfo = obj._debugInfo;
50-
if (debugInfo) {
51-
const copy = [];
52-
for (let i = 0; i < debugInfo.length; i++) {
53-
copy.push(normalizeComponentInfo(debugInfo[i]));
54-
}
55-
return copy;
56-
}
57-
return debugInfo;
58-
}
59-
6021
let act;
6122
let React;
6223
let ReactNoop;
6324
let ReactNoopFlightServer;
6425
let ReactNoopFlightClient;
26+
let getDebugInfo;
6527

6628
describe('ReactFlight', () => {
6729
beforeEach(() => {
@@ -91,6 +53,11 @@ describe('ReactFlight', () => {
9153
ReactNoop = require('react-noop-renderer');
9254
ReactNoopFlightClient = require('react-noop-renderer/flight-client');
9355
act = require('internal-test-utils').act;
56+
57+
getDebugInfo = require('internal-test-utils').getDebugInfo.bind(null, {
58+
useV8Stack: true,
59+
ignoreRscStreamInfo: true,
60+
});
9461
});
9562

9663
afterEach(() => {

0 commit comments

Comments
 (0)