[DevTools] Regression Test Jest Config (#24598)
Some older React versions have different module import names and are missing certain features. This PR mocks modules that don't exist and maps modules in older versions to the ones that are required in tests. Specifically: * In React v16.5, scheduler is named schedule * In pre concurrent React, there is no act * Prior to React v18.0, react-dom/client doesn't exist * In DevTools, we expect to use scheduler/tracing-profiling instead of scheduler/tracing
Luna Ruan committed
May 23, 2022 at 10:50 UTC
5a1e558df21bd3cafbaea01cc418fa69d14a8cab
3 files changed
+77
-1
scripts/jest/config.build-devtools.js
+6
-1
@@ -3,6 +3,7 @@
3
const {readdirSync, statSync} = require('fs');
4
const {join} = require('path');
5
const baseConfig = require('./config.base');
6
+const devtoolsRegressionConfig = require('./devtools/config.build-devtools-regression');
7
8
const NODE_MODULES_DIR =
9
process.env.RELEASE_CHANNEL === 'stable' ? 'oss-stable' : 'oss-experimental';
@@ -49,7 +50,10 @@ moduleNameMapper['^react-reconciler/([^/]+)$'] =
50
51
module.exports = Object.assign({}, baseConfig, {
52
// Redirect imports to the compiled bundles
52
- moduleNameMapper,
53
+ moduleNameMapper: {
54
+ ...moduleNameMapper,
55
+ ...devtoolsRegressionConfig.moduleNameMapper,
56
+ },
57
// Don't run bundle tests on -test.internal.* files
58
testPathIgnorePatterns: ['/node_modules/', '-test.internal.js$'],
59
// Exclude the build output from transforms
@@ -88,6 +92,7 @@ module.exports = Object.assign({}, baseConfig, {
92
],
93
setupFiles: [
94
...baseConfig.setupFiles,
95
+ ...devtoolsRegressionConfig.setupFiles,
96
require.resolve('./setupTests.build.js'),
97
require.resolve('./devtools/setupEnv.js'),
98
],
scripts/jest/devtools/config.build-devtools-regression.js
new
+41
@@ -0,0 +1,41 @@
1
+'use strict';
2
+
3
+const semver = require('semver');
4
+
5
+const NODE_MODULES_DIR =
6
+ process.env.RELEASE_CHANNEL === 'stable' ? 'oss-stable' : 'oss-experimental';
7
+
8
+const REACT_VERSION = process.env.REACT_VERSION;
9
+
10
+const moduleNameMapper = {};
11
+const setupFiles = [];
12
+
13
+// We only want to add these if we are in a regression test, IE if there
14
+// is a REACT_VERSION specified
15
+if (REACT_VERSION) {
16
+ // React version 16.5 has a schedule package instead of a scheduler
17
+ // package, so we need to rename them accordingly
18
+ if (semver.satisfies(REACT_VERSION, '16.5')) {
19
+ moduleNameMapper[
20
+ `^schedule$`
21
+ ] = `<rootDir>/build/${NODE_MODULES_DIR}/schedule`;
22
+ moduleNameMapper[
23
+ '^schedule/tracing$'
24
+ ] = `<rootDir>/build/${NODE_MODULES_DIR}/schedule/tracing-profiling`;
25
+ }
26
+
27
+ // react-dom/client is only in v18.0.0 and up, so we
28
+ // map it to react-dom instead
29
+ if (semver.satisfies(REACT_VERSION, '<18.0')) {
30
+ moduleNameMapper[
31
+ '^react-dom/client$'
32
+ ] = `<rootDir>/build/${NODE_MODULES_DIR}/react-dom`;
33
+ }
34
+
35
+ setupFiles.push(require.resolve('./setupTests.build-devtools-regression'));
36
+}
37
+
38
+module.exports = {
39
+ moduleNameMapper,
40
+ setupFiles,
41
+};
scripts/jest/devtools/setupTests.build-devtools-regression.js
new
+30
@@ -0,0 +1,30 @@
1
+'use strict';
2
+
3
+// Regression tests use a React DOM profiling, so we need
4
+// to replace these tests with scheduler/tracing-profiling
5
+jest.mock('scheduler/tracing', () => {
6
+ return jest.requireActual('scheduler/tracing-profiling');
7
+});
8
+
9
+// act doesn't exist in older versions of React, but
10
+// DevTools tests sometimes import and depend on act to run.
11
+// If act doesn't exist for a particular version of React, we will
12
+// mock it with a function. This should work in most tests
13
+// that we want to call with older versions of React.
14
+// TODO (luna) Refactor act in DevTools test utils to not depend
15
+// on act in react-dom or react-test-renderer so we don't need to do this
16
+jest.mock('react-test-renderer', () => {
17
+ const reactTestRenderer = jest.requireActual('react-test-renderer');
18
+ if (!reactTestRenderer.act) {
19
+ reactTestRenderer.act = fn => fn();
20
+ }
21
+ return reactTestRenderer;
22
+});
23
+
24
+jest.mock('react-dom/test-utils', () => {
25
+ const testUtils = jest.requireActual('react-dom/test-utils');
26
+ if (!testUtils.act) {
27
+ testUtils.act = fn => fn();
28
+ }
29
+ return testUtils;
30
+});