Skip to content

Commit 1dd4d8e

Browse files
rickhanloniiAndyPengc12
authored andcommitted
Use react@17 for useSyncExternalStore shim tests (facebook#28055)
The tests for the shim need to test with ReactDOM.render in React 17.
1 parent ff931bf commit 1dd4d8e

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

packages/use-sync-external-store/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
"license": "MIT",
2121
"peerDependencies": {
2222
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
23+
},
24+
"devDependencies": {
25+
"react-17": "npm:react@^17",
26+
"react-dom-17": "npm:react-dom@^17"
2327
}
2428
}

packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,21 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
2929
jest.resetModules();
3030

3131
if (gate(flags => flags.enableUseSyncExternalStoreShim)) {
32-
// Remove useSyncExternalStore from the React imports so that we use the
33-
// shim instead. Also removing startTransition, since we use that to
34-
// detect outdated 18 alphas that don't yet include useSyncExternalStore.
35-
//
36-
// Longer term, we'll probably test this branch using an actual build
37-
// of React 17.
32+
// Test the shim against React 17.
3833
jest.mock('react', () => {
39-
const {
40-
// eslint-disable-next-line no-unused-vars
41-
startTransition: _,
42-
// eslint-disable-next-line no-unused-vars
43-
useSyncExternalStore: __,
44-
...otherExports
45-
} = jest.requireActual('react');
46-
return otherExports;
34+
return jest.requireActual(
35+
__DEV__
36+
? 'react-17/umd/react.development.js'
37+
: 'react-17/umd/react.production.min.js',
38+
);
4739
});
40+
jest.mock('react-dom', () =>
41+
jest.requireActual(
42+
__DEV__
43+
? 'react-dom-17/umd/react-dom.development.js'
44+
: 'react-dom-17/umd/react-dom.production.min.js',
45+
),
46+
);
4847
}
4948

5049
React = require('react');
@@ -100,6 +99,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
10099
// createLegacyRoot directly.
101100
return ReactDOMClient.createRoot(container);
102101
} else {
102+
// This ReactDOM.render is from the React 17 npm module.
103103
ReactDOM.render(null, container);
104104
return {
105105
render(children) {
@@ -594,6 +594,15 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
594594
);
595595
}).toErrorDev(
596596
'The result of getSnapshot should be cached to avoid an infinite loop',
597+
{
598+
withoutStack: gate(flags => {
599+
if (flags.enableUseSyncExternalStoreShim) {
600+
// Stacks don't work when mixing the source and the npm package.
601+
return flags.source;
602+
}
603+
return false;
604+
}),
605+
},
597606
);
598607
});
599608

scripts/jest/setupTests.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,14 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
170170
return message;
171171
}
172172
const re = /react.dev\/errors\/(\d+)?\??([^\s]*)/;
173-
const matches = message.match(re);
173+
let matches = message.match(re);
174174
if (!matches || matches.length !== 3) {
175-
return message;
175+
// Some tests use React 17, when the URL was different.
176+
const re17 = /error-decoder.html\?invariant=(\d+)([^\s]*)/;
177+
matches = message.match(re17);
178+
if (!matches || matches.length !== 3) {
179+
return message;
180+
}
176181
}
177182
const code = parseInt(matches[1], 10);
178183
const args = matches[2]

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12838,6 +12838,14 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.2.8:
1283812838
prop-types "^15.6.2"
1283912839
scheduler "^0.13.0"
1284012840

12841+
"react-17@npm:react@^17":
12842+
version "17.0.2"
12843+
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"
12844+
integrity sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==
12845+
dependencies:
12846+
loose-envify "^1.1.0"
12847+
object-assign "^4.1.1"
12848+
1284112849
"react-dom-15@npm:react-dom@^15":
1284212850
version "15.6.2"
1284312851
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-15.6.2.tgz#41cfadf693b757faf2708443a1d1fd5a02bef730"
@@ -12858,6 +12866,15 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.2.8:
1285812866
prop-types "^15.6.2"
1285912867
scheduler "^0.13.0"
1286012868

12869+
"react-dom-17@npm:react-dom@^17":
12870+
version "17.0.2"
12871+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.2.tgz#ecffb6845e3ad8dbfcdc498f0d0a939736502c23"
12872+
integrity sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==
12873+
dependencies:
12874+
loose-envify "^1.1.0"
12875+
object-assign "^4.1.1"
12876+
scheduler "^0.20.2"
12877+
1286112878
react-is@^16.8.1, react-is@^17.0.1, "react-is@npm:react-is":
1286212879
version "17.0.2"
1286312880
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
@@ -13524,6 +13541,14 @@ scheduler@^0.13.0:
1352413541
loose-envify "^1.1.0"
1352513542
object-assign "^4.1.1"
1352613543

13544+
scheduler@^0.20.2:
13545+
version "0.20.2"
13546+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.20.2.tgz#4baee39436e34aa93b4874bddcbf0fe8b8b50e91"
13547+
integrity sha512-2eWfGgAqqWFGqtdMmcL5zCMK1U8KlXv8SQFGglL3CEtd0aDVDWgeF/YoCmvln55m5zSk3J/20hTaSBeSObsQDQ==
13548+
dependencies:
13549+
loose-envify "^1.1.0"
13550+
object-assign "^4.1.1"
13551+
1352713552
schema-utils@^1.0.0:
1352813553
version "1.0.0"
1352913554
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-1.0.0.tgz#0b79a93204d7b600d4b2850d1f66c2a34951c770"

0 commit comments

Comments
 (0)