Use react@17 for useSyncExternalStore shim tests (#28055)
The tests for the shim need to test with ReactDOM.render in React 17.
Ricky committed
Jan 24, 2024 at 11:27 UTC
ee1eb4826f4f7beb4e06f2c15f03145bb5bddf5b
4 files changed
+59
-16
packages/use-sync-external-store/package.json
+4
@@ -20,5 +20,9 @@
20
"license": "MIT",
21
"peerDependencies": {
22
"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"
27
}
28
}
packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js
+23
-14
@@ -29,22 +29,21 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
29
jest.resetModules();
30
31
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.
33
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
+ );
39
});
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
+ );
47
}
48
49
React = require('react');
@@ -100,6 +99,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
99
// createLegacyRoot directly.
100
return ReactDOMClient.createRoot(container);
101
} else {
102
+ // This ReactDOM.render is from the React 17 npm module.
103
ReactDOM.render(null, container);
104
return {
105
render(children) {
@@ -594,6 +594,15 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
594
);
595
}).toErrorDev(
596
'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
+ },
606
);
607
});
608
scripts/jest/setupTests.js
+7
-2
@@ -170,9 +170,14 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
170
return message;
171
}
172
const re = /react.dev\/errors\/(\d+)?\??([^\s]*)/;
173
- const matches = message.match(re);
173
+ let matches = message.match(re);
174
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
+ }
181
}
182
const code = parseInt(matches[1], 10);
183
const args = matches[2]
yarn.lock
+25
@@ -12838,6 +12838,14 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.2.8:
12838
prop-types "^15.6.2"
12839
scheduler "^0.13.0"
12840
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
+
12849
"react-dom-15@npm:react-dom@^15":
12850
version "15.6.2"
12851
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:
12866
prop-types "^15.6.2"
12867
scheduler "^0.13.0"
12868
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
+
12878
react-is@^16.8.1, react-is@^17.0.1, "react-is@npm:react-is":
12879
version "17.0.2"
12880
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
@@ -13524,6 +13541,14 @@ scheduler@^0.13.0:
13541
loose-envify "^1.1.0"
13542
object-assign "^4.1.1"
13543
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
+
13552
schema-utils@^1.0.0:
13553
version "1.0.0"
13554
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-1.0.0.tgz#0b79a93204d7b600d4b2850d1f66c2a34951c770"