Skip to content

Commit 32118f1

Browse files
committed
wip: setup unit test to run with react v18 and v19
1 parent 71b1ac7 commit 32118f1

8 files changed

+63
-2
lines changed

.circleci/config.yml

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
- image: cimg/node:22.12.0-browsers
5050
working_directory: ~/repo
5151
resource_class: medium+
52+
parameters:
53+
react-major-version:
54+
type: string
5255
steps:
5356
- checkout
5457
- pnpm_setup
@@ -57,6 +60,7 @@ jobs:
5760
command: pnpm test
5861
environment:
5962
JEST_JUNIT_OUTPUT: 'test-reports/junit/js-test-results.xml'
63+
REACT_MAJOR_VERSION: << parameters.react-major-version >>
6064

6165
- store_test_results:
6266
path: test-reports/junit
@@ -139,6 +143,9 @@ workflows:
139143
requires:
140144
- install
141145
- test-unit:
146+
matrix:
147+
parameters:
148+
react-major-version: ['18', '19']
142149
requires:
143150
- install
144151
- test-bundle:

csp-server/environment.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
interface ProcessEnv {
2-
NODE_ENV?: 'development' | 'production';
32
CI?: boolean;
3+
NODE_ENV?: 'development' | 'production';
4+
REACT_MAJOR_VERSION?: '18' | '19';
45
}
56

67
interface Process {

jest.config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
/// <reference path="./test/typings/environment.d.ts" />
33

44
import type { Config } from 'jest';
5+
import getReactMajorVersion from './test/util/get-react-major-version';
56
import isRunningInCI from './test/util/is-running-in-ci';
67

8+
const reactMajorVersion = getReactMajorVersion();
9+
710
const config: Config = {
811
clearMocks: true,
912
modulePathIgnorePatterns: ['/dist/'],
@@ -26,6 +29,17 @@ const config: Config = {
2629
],
2730
};
2831

32+
// eslint-disable-next-line no-console
33+
console.log('Testing with React version:', `${reactMajorVersion}.x.x`);
34+
35+
if (reactMajorVersion === '18') {
36+
config.cacheDirectory = `.cache/jest-cache-react-${reactMajorVersion}`;
37+
config.moduleNameMapper = {
38+
'^react-dom((\\/.*)?)$': `react-dom-${reactMajorVersion}$1`,
39+
'^react((\\/.*)?)$': `react-${reactMajorVersion}$1`,
40+
};
41+
}
42+
2943
if (isRunningInCI()) {
3044
config.maxWorkers = 2;
3145
}

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
"release:test": "release-it --dry-run",
5050
"test:accessibility": "lighthouse http://localhost:9002/iframe.html?id=examples-single-vertical-list--basic --no-enable-error-reporting --config-path=lighthouse.config.js --chrome-flags='--headless' --output=json --output=html --output-path=./test-reports/lighthouse/a11y.json && node a11y-audit-parse.js",
5151
"test": "jest --config ./jest.config.ts",
52+
"test:react-18": "cross-env REACT_MAJOR_VERSION=18 pnpm test",
53+
"test:react-19": "cross-env REACT_MAJOR_VERSION=19 pnpm test",
5254
"test:browser": "cypress open",
5355
"test:browser:ci": "cypress run",
5456
"test:coverage": "pnpm test --coverage --coveragePathIgnorePatterns=/debug",
@@ -175,7 +177,9 @@
175177
"prettier": "3.4.2",
176178
"raf-stub": "3.0.0",
177179
"react": "19.0.0",
180+
"react-18": "npm:react@18.3.0",
178181
"react-dom": "19.0.0",
182+
"react-dom-18": "npm:react-dom@18.3.0",
179183
"react-window": "1.8.11",
180184
"release-it": "17.11.0",
181185
"require-from-string": "2.0.2",

pnpm-lock.yaml

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

renovate.json5

+6
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@
4848
rangeStrategy: "widen",
4949
commitMessagePrefix: "chore(peer-deps):",
5050
},
51+
52+
// to support previous react version
53+
{
54+
"matchPackageNames": ["react-18", "react-dom-18"],
55+
"allowedVersions": "<19.0.0",
56+
},
5157
],
5258
}

test/typings/environment.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
interface ProcessEnv {
2-
NODE_ENV?: 'development' | 'production';
32
CI?: boolean;
3+
NODE_ENV?: 'development' | 'production';
4+
REACT_MAJOR_VERSION?: '18' | '19';
45
}
56

67
interface Process {

test/util/get-react-major-version.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function getReactMajorVersion(): '18' | '19' {
2+
return process.env.REACT_MAJOR_VERSION || '19';
3+
}

0 commit comments

Comments
 (0)