Skip to content

Commit e439bd1

Browse files
committed
Current behavior for what happens if you resolve before React gets a chance to process the ping
1 parent 8d22556 commit e439bd1

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

packages/react-server/src/__tests__/ReactServer-test.js

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,38 @@ let ReactNoopServer;
1818
function normalizeCodeLocInfo(str) {
1919
return (
2020
str &&
21-
str
22-
.split('\n')
23-
.filter(frame => {
24-
// These frames should be ignore-listed since they point into
25-
// React internals i.e. node_modules.
26-
return (
27-
frame.indexOf('ReactFizzHooks') === -1 &&
28-
frame.indexOf('ReactFizzThenable') === -1 &&
29-
frame.indexOf('ReactHooks') === -1
30-
);
31-
})
32-
.join('\n')
33-
.replace(/^ +(?:at|in) ([\S]+)[^\n]*/gm, function (m, name) {
34-
const dot = name.lastIndexOf('.');
35-
if (dot !== -1) {
36-
name = name.slice(dot + 1);
37-
}
38-
return ' in ' + name + (/\d/.test(m) ? ' (at **)' : '');
39-
})
21+
str.replace(/^ +(?:at|in) ([\S]+)[^\n]*/gm, function (m, name) {
22+
const dot = name.lastIndexOf('.');
23+
if (dot !== -1) {
24+
name = name.slice(dot + 1);
25+
}
26+
return ' in ' + name + (/\d/.test(m) ? ' (at **)' : '');
27+
})
4028
);
4129
}
4230

31+
/**
32+
* Removes all stackframes not pointing into this file
33+
*/
34+
function ignoreListStack(str) {
35+
if (!str) {
36+
return str;
37+
}
38+
39+
let ignoreListedStack = '';
40+
const lines = str.split('\n');
41+
42+
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
43+
for (const line of lines) {
44+
if (line.indexOf(__filename) === -1) {
45+
} else {
46+
ignoreListedStack += '\n' + line.replace(__dirname, '.');
47+
}
48+
}
49+
50+
return ignoreListedStack;
51+
}
52+
4353
const currentTask = new AsyncLocalStorage({defaultValue: null});
4454

4555
describe('ReactServer', () => {
@@ -75,26 +85,28 @@ describe('ReactServer', () => {
7585
it('has Owner Stacks in DEV when aborted', async () => {
7686
const Context = React.createContext(null);
7787

78-
function Component({promise}) {
88+
function Component({p1, p2, p3}) {
7989
const context = React.use(Context);
8090
if (context === null) {
8191
throw new Error('Missing context');
8292
}
83-
React.use(promise);
93+
React.use(p1);
94+
React.use(p2);
95+
React.use(p3);
8496
return <div>Hello, Dave!</div>;
8597
}
86-
function Indirection({promise}) {
98+
function Indirection({p1, p2, p3}) {
8799
return (
88100
<div>
89-
<Component promise={promise} />
101+
<Component p1={p1} p2={p2} p3={p3} />
90102
</div>
91103
);
92104
}
93-
function App({promise}) {
105+
function App({p1, p2, p3}) {
94106
return (
95107
<section>
96108
<div>
97-
<Indirection promise={promise} />
109+
<Indirection p1={p1} p2={p2} p3={p3} />
98110
</div>
99111
</section>
100112
);
@@ -104,9 +116,21 @@ describe('ReactServer', () => {
104116
let componentStack;
105117
let ownerStack;
106118
let task;
119+
const resolvedPromise = Promise.resolve('one');
120+
resolvedPromise.status = 'fulfilled';
121+
resolvedPromise.value = 'one';
122+
let resolvePendingPromise;
123+
const pendingPromise = new Promise(resolve => {
124+
resolvePendingPromise = value => {
125+
pendingPromise.status = 'fulfilled';
126+
pendingPromise.value = value;
127+
resolve(value);
128+
};
129+
});
130+
const hangingPromise = new Promise(() => {});
107131
const result = ReactNoopServer.render(
108132
<Context value="provided">
109-
<App promise={new Promise(() => {})} />
133+
<App p1={resolvedPromise} p2={pendingPromise} p3={hangingPromise} />
110134
</Context>,
111135
{
112136
onError: (error, errorInfo) => {
@@ -119,6 +143,7 @@ describe('ReactServer', () => {
119143
);
120144

121145
await act(async () => {
146+
resolvePendingPromise('two');
122147
result.abort();
123148
});
124149
expect(caughtError).toEqual(
@@ -134,12 +159,14 @@ describe('ReactServer', () => {
134159
'\n in section' +
135160
'\n in App (at **)',
136161
);
137-
expect(normalizeCodeLocInfo(ownerStack)).toEqual(
162+
expect(ignoreListStack(ownerStack)).toEqual(
138163
__DEV__
139164
? '' +
140-
'\n in Component (at **)' +
141-
'\n in Indirection (at **)' +
142-
'\n in App (at **)'
165+
// The concrete location may change as this test is updated.
166+
// Just make sure they still point at the same code
167+
'\n at Component (./ReactServer-test.js:95:13)' +
168+
'\n at Indirection (./ReactServer-test.js:101:44)' +
169+
'\n at App (./ReactServer-test.js:109:46)'
143170
: null,
144171
);
145172
expect(task).toEqual(__DEV__ ? '\n<Component>' : null);

0 commit comments

Comments
 (0)