Skip to content

Commit f196d2f

Browse files
authored
test: migrate react tests to vitest (#716)
* test: migrate react tests to vitest * test: migrate react-transform to vitest * chore: remove mocha/sinon/chai/karma * fix: add exact import * fix: strongly type mocked components * test: add the sketchy transform back * fix: funky runtime path * fix: resolve signals after runtime * test: map things to places * test: use vitest aliases THIS MAY ACTUALLY WORK! * chore: use react 16 in runtime too
1 parent 2830b0e commit f196d2f

28 files changed

+357
-1425
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ jobs:
4646
- name: Test react production build
4747
run: pnpm test:prod
4848

49+
- name: Prepare runtime with React 16
50+
working-directory: packages/react/runtime
51+
run: |
52+
pnpm i react@16 react-dom@16 react-router-dom@5
53+
4954
- name: Test React 16
5055
working-directory: packages/react
5156
run: |

karma.conf.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,7 @@ function createEsbuildPlugin(filteredPkgList) {
226226
}
227227

228228
const pkgList = {
229-
react: "@preact/signals-react",
230-
"react/utils": "@preact/signals-react/utils",
231229
"react/runtime": "@preact/signals-react/runtime",
232-
"react-transform": "@preact/signals-react-transform",
233230
};
234231

235232
module.exports = function (config) {

mangle.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
},
66
"minify": {
77
"mangle": {
8-
"reserved": [
9-
"useSignal",
10-
"useComputed",
11-
"useSignalEffect"
12-
],
8+
"reserved": ["useSignal", "useComputed", "useSignalEffect"],
139
"keep_classnames": true,
1410
"properties": {
1511
"regex": "^_[^_]",

package.json

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,11 @@
2222
"lint:oxlint": "oxlint 'packages/**/*.{ts,tsx,js,jsx}'",
2323
"lint:tsc": "tsc -p tsconfig.json --noEmit",
2424
"lint-staged": "lint-staged",
25-
"test": "pnpm test:karma && pnpm test:mocha && pnpm test:vitest",
26-
"test:minify": "pnpm test:karma:minify && pnpm test:mocha",
27-
"test:prod": "pnpm test:karma:prod && pnpm test:mocha:prod",
28-
"test:karma": "cross-env COVERAGE=true karma start karma.conf.js --single-run",
29-
"test:karma:minify": "cross-env COVERAGE=true MINIFY=true karma start karma.conf.js --single-run",
30-
"test:karma:watch": "karma start karma.conf.js --no-single-run",
31-
"test:karma:prod": "cross-env MINIFY=true NODE_ENV=production karma start karma.conf.js --single-run",
32-
"test:karma:prod:watch": "cross-env NODE_ENV=production karma start karma.conf.js --no-single-run",
33-
"test:mocha": "cross-env COVERAGE=true mocha --require test/node/setup.js --recursive packages/*/test/node/**.test.tsx --ignore \"packages/{preact/utils,preact,core}\"",
34-
"test:mocha:prod": "cross-env COVERAGE=true NODE_ENV=production mocha --require test/node/setup.js --recursive packages/*/test/node/**.test.tsx --ignore \"packages/{preact/utils,preact,core}\"",
25+
"test": "pnpm test:vitest",
26+
"test:minify": "pnpm test:vitest:prod",
27+
"test:prod": "pnpm test:vitest:prod",
3528
"test:vitest": "cross-env COVERAGE=true vitest run",
29+
"test:vitest:prod": "cross-env COVERAGE=true MINIFY=true vitest run",
3630
"docs:start": "cd docs && pnpm start",
3731
"docs:build": "cd docs && pnpm build",
3832
"docs:preview": "cd docs && pnpm preview",
@@ -60,40 +54,25 @@
6054
"@babel/standalone": "^7.27.7",
6155
"@changesets/changelog-github": "^0.5.0",
6256
"@changesets/cli": "^2.27.1",
57+
"@preact/signals-react-transform": "workspace:*",
6358
"@types/babel__traverse": "^7.18.5",
64-
"@types/chai": "^4.3.3",
65-
"@types/mocha": "^9.1.1",
6659
"@types/node": "^18.19.103",
67-
"@types/sinon": "^10.0.13",
68-
"@types/sinon-chai": "^3.2.8",
6960
"@vitest/browser": "^3.2.4",
7061
"@vitest/coverage-v8": "^3.2.4",
7162
"babel-plugin-istanbul": "^6.1.1",
7263
"babel-plugin-transform-rename-properties": "^0.1.0",
7364
"buffer": "^6.0.3",
74-
"chai": "^4.3.6",
7565
"cross-env": "^7.0.3",
7666
"errorstacks": "^2.4.0",
7767
"esbuild": "^0.14.54",
7868
"husky": "^8.0.1",
79-
"karma": "6.4.2",
80-
"karma-chai-sinon": "^0.1.5",
81-
"karma-chrome-launcher": "^3.1.1",
82-
"karma-coverage": "^2.2.0",
83-
"karma-esbuild": "^2.2.5",
84-
"karma-mocha": "^2.0.1",
85-
"karma-mocha-reporter": "^2.2.5",
86-
"karma-sinon": "^1.0.5",
8769
"kolorist": "^1.5.1",
8870
"lint-staged": "^14.0.1",
8971
"microbundle": "^0.15.1",
90-
"mocha": "^10.0.0",
9172
"oxlint": "^1.3.0",
9273
"playwright": "^1.53.1",
9374
"prettier": "^3.6.2",
9475
"shx": "^0.3.4",
95-
"sinon": "^14.0.0",
96-
"sinon-chai": "^3.7.0",
9776
"typescript": "~5.8.3",
9877
"vitest": "^3.2.4",
9978
"vite": "^6.3.5"

packages/react-transform/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
},
5858
"devDependencies": {
5959
"@babel/core": "^7.22.8",
60+
"@preact/signals-core": "workspace:*",
6061
"@types/babel__core": "^7.20.1",
6162
"@types/babel__helper-module-imports": "^7.18.0",
6263
"@types/babel__helper-plugin-utils": "^7.10.0",

packages/react-transform/test/browser/e2e.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
createRoot,
1414
getConsoleErrorSpy,
1515
} from "../../../react/test/shared/utils";
16+
import { describe, beforeEach, afterEach, expect, it } from "vitest";
17+
import "../../../../test/browser/babel.js";
1618

1719
const customSource = "useSignals-custom-source";
1820
const modules: Record<string, any> = {
@@ -32,8 +34,6 @@ function testRequire(name: string) {
3234
}
3335

3436
async function createComponent(code: string, options?: PluginOptions) {
35-
// `transformSignalCode` is a global helper function added to the global
36-
// namespace by a test helper we've included in the Karma config.
3737
const cjsCode = transformSignalCode(code, options);
3838
// console.log(cjsCode); // Useful when debugging tests.
3939

@@ -55,7 +55,7 @@ describe("React Signals babel transfrom - browser E2E tests", () => {
5555
scratch = document.createElement("div");
5656
document.body.appendChild(scratch);
5757
root = await createRoot(scratch);
58-
getConsoleErrorSpy().resetHistory();
58+
getConsoleErrorSpy().mockClear();
5959
});
6060

6161
afterEach(async () => {

packages/react-transform/test/node/index.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
objMethodComp,
2020
variableHooks,
2121
} from "./helpers";
22+
import { it, describe, expect } from "vitest";
2223

2324
// To help interactively debug a specific test case, add the test ids of the
2425
// test cases you want to debug to the `debugTestIds` array, e.g. (["258",

packages/react/runtime/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"@preact/signals-core": "workspace:^1.3.0",
2222
"use-sync-external-store": "^1.2.0"
2323
},
24+
"devDependencies": {
25+
"@preact/signals-react": "workspace:*"
26+
},
2427
"peerDependencies": {
2528
"react": "^16.14.0 || 17.x || 18.x"
2629
}

packages/react/runtime/test/browser/mounts.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
createRoot,
1010
type Root,
1111
} from "../../../test/shared/utils.js";
12+
import { describe, beforeEach, afterEach } from "vitest";
1213

1314
describe("@preact/signals-react/runtime", () => {
1415
describe("mounting", () => {
@@ -25,7 +26,7 @@ describe("@preact/signals-react/runtime", () => {
2526
beforeEach(async () => {
2627
scratch = document.createElement("div");
2728
document.body.appendChild(scratch);
28-
getConsoleErrorSpy().resetHistory();
29+
getConsoleErrorSpy().mockClear();
2930

3031
root = await createRoot(scratch);
3132
});

packages/react/runtime/test/browser/suspense.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
checkHangingAct,
1616
getConsoleErrorSpy,
1717
} from "../../../test/shared/utils";
18+
import { beforeEach, afterEach, describe, it, expect } from "vitest";
1819

1920
describe("Suspense", () => {
2021
let scratch: HTMLDivElement;
@@ -28,7 +29,7 @@ describe("Suspense", () => {
2829
scratch = document.createElement("div");
2930
document.body.appendChild(scratch);
3031
root = await createRoot(scratch);
31-
getConsoleErrorSpy().resetHistory();
32+
getConsoleErrorSpy().mockClear();
3233
});
3334

3435
afterEach(async () => {

0 commit comments

Comments
 (0)