Skip to content

Commit

Permalink
wip: setup unit test to run with react v18 and v19
Browse files Browse the repository at this point in the history
  • Loading branch information
100terres committed Jan 22, 2025
1 parent 71b1ac7 commit 32118f1
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jobs:
- image: cimg/node:22.12.0-browsers
working_directory: ~/repo
resource_class: medium+
parameters:
react-major-version:
type: string
steps:
- checkout
- pnpm_setup
Expand All @@ -57,6 +60,7 @@ jobs:
command: pnpm test
environment:
JEST_JUNIT_OUTPUT: 'test-reports/junit/js-test-results.xml'
REACT_MAJOR_VERSION: << parameters.react-major-version >>

- store_test_results:
path: test-reports/junit
Expand Down Expand Up @@ -139,6 +143,9 @@ workflows:
requires:
- install
- test-unit:
matrix:
parameters:
react-major-version: ['18', '19']
requires:
- install
- test-bundle:
Expand Down
3 changes: 2 additions & 1 deletion csp-server/environment.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface ProcessEnv {
NODE_ENV?: 'development' | 'production';
CI?: boolean;
NODE_ENV?: 'development' | 'production';
REACT_MAJOR_VERSION?: '18' | '19';
}

interface Process {
Expand Down
14 changes: 14 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
/// <reference path="./test/typings/environment.d.ts" />

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

const reactMajorVersion = getReactMajorVersion();

const config: Config = {
clearMocks: true,
modulePathIgnorePatterns: ['/dist/'],
Expand All @@ -26,6 +29,17 @@ const config: Config = {
],
};

// eslint-disable-next-line no-console
console.log('Testing with React version:', `${reactMajorVersion}.x.x`);

if (reactMajorVersion === '18') {
config.cacheDirectory = `.cache/jest-cache-react-${reactMajorVersion}`;
config.moduleNameMapper = {
'^react-dom((\\/.*)?)$': `react-dom-${reactMajorVersion}$1`,
'^react((\\/.*)?)$': `react-${reactMajorVersion}$1`,
};
}

if (isRunningInCI()) {
config.maxWorkers = 2;
}
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"release:test": "release-it --dry-run",
"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",
"test": "jest --config ./jest.config.ts",
"test:react-18": "cross-env REACT_MAJOR_VERSION=18 pnpm test",
"test:react-19": "cross-env REACT_MAJOR_VERSION=19 pnpm test",
"test:browser": "cypress open",
"test:browser:ci": "cypress run",
"test:coverage": "pnpm test --coverage --coveragePathIgnorePatterns=/debug",
Expand Down Expand Up @@ -175,7 +177,9 @@
"prettier": "3.4.2",
"raf-stub": "3.0.0",
"react": "19.0.0",
"react-18": "npm:react@18.3.0",
"react-dom": "19.0.0",
"react-dom-18": "npm:react-dom@18.3.0",
"react-window": "1.8.11",
"release-it": "17.11.0",
"require-from-string": "2.0.2",
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@
rangeStrategy: "widen",
commitMessagePrefix: "chore(peer-deps):",
},

// to support previous react version
{
"matchPackageNames": ["react-18", "react-dom-18"],
"allowedVersions": "<19.0.0",
},
],
}
3 changes: 2 additions & 1 deletion test/typings/environment.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface ProcessEnv {
NODE_ENV?: 'development' | 'production';
CI?: boolean;
NODE_ENV?: 'development' | 'production';
REACT_MAJOR_VERSION?: '18' | '19';
}

interface Process {
Expand Down
3 changes: 3 additions & 0 deletions test/util/get-react-major-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getReactMajorVersion(): '18' | '19' {
return process.env.REACT_MAJOR_VERSION || '19';
}

0 comments on commit 32118f1

Please sign in to comment.