Skip to content

Commit 956383a

Browse files
committed
chore: migrate from mocha to vitest
Removes mocha and runs all tests in vitest. Notable changes: - TypeScript tests are still run via `tsc`, but the associated test suites are now run in vitest _node mode_ - Shared tests are now run in both _browser mode_ and _node mode_ - Node tests are now run in _node mode_ The type tests have also been renamed to `*.test-d.tsx`, to make them easier to exclude and in case we ever switch to vitest's type testing feature.
1 parent 8bd2240 commit 956383a

10 files changed

+70
-619
lines changed

package-lock.json

Lines changed: 0 additions & 568 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,13 @@
125125
"dev:hooks": "microbundle watch --raw --no-generateTypes --format cjs --cwd hooks",
126126
"dev:compat": "microbundle watch --raw --no-generateTypes --format cjs --cwd compat --globals 'preact/hooks=preactHooks'",
127127
"test": "npm-run-all build lint test:unit",
128-
"test:unit": "run-p test:mocha test:vitest:min test:ts",
128+
"test:unit": "run-p test:vitest:min test:ts",
129129
"test:vitest": "cross-env COVERAGE=true vitest run",
130130
"test:vitest:min": "cross-env MINIFY=true vitest run",
131131
"test:vitest:watch": "vitest",
132132
"test:ts": "run-p 'test:ts:*'",
133-
"test:ts:core": "tsc -p test/ts/ && mocha --require \"@babel/register\" test/ts/**/*-test.js",
133+
"test:ts:core": "tsc -p test/ts/",
134134
"test:ts:compat": "tsc -p compat/test/ts/",
135-
"test:mocha": "mocha --recursive --require \"@babel/register\" test/shared test/node",
136-
"test:mocha:watch": "npm run test:mocha -- --watch",
137135
"lint": "run-s oxlint tsc",
138136
"tsc": "tsc -p jsconfig-lint.json",
139137
"oxlint": "oxlint -c oxlint.json src test/browser test/node test/shared debug compat hooks test-utils",
@@ -209,7 +207,6 @@
209207
"@babel/register": "^7.25.9",
210208
"@biomejs/biome": "^1.9.4",
211209
"@types/chai": "^5.0.1",
212-
"@types/mocha": "^10.0.0",
213210
"@types/node": "^18.19.87",
214211
"@types/sinon": "^17.0.3",
215212
"@vitest/browser": "^3.2.1",
@@ -223,7 +220,6 @@
223220
"husky": "^9.1.7",
224221
"kolorist": "^1.8.0",
225222
"microbundle": "^0.15.1",
226-
"mocha": "^11.0.0",
227223
"npm-run-all2": "^7.0.0",
228224
"oxlint": "^0.15.12",
229225
"preact-render-to-string": "^6.5.0",
File renamed without changes.
File renamed without changes.

test/ts/dom-attributes-test.tsx renamed to test/ts/dom-attributes.test-d.tsx

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,20 @@ const validAriaRole = <button role="slider" />;
1818
const signalBadAriaRole = (
1919
// @ts-expect-error A button should not have a role of presentation
2020
<button role={createSignal('presentation' as const)} />
21-
)
22-
const signalValidAriaRole = (
23-
<button role={createSignal('slider' as const)} />
24-
)
21+
);
22+
const signalValidAriaRole = <button role={createSignal('slider' as const)} />;
2523

2624
// @ts-expect-error A map should never have any role set
2725
const invalidAriaRole = <map role="presentation" />;
2826
const signalInvalidAriaRole = (
2927
// @ts-expect-error A map should never have any role set
3028
<button role={createSignal('presentation' as const)} />
31-
)
32-
const validMissingAriaRole = <base href=""></base>
29+
);
30+
const validMissingAriaRole = <base href=""></base>;
3331
const signalValidMissingAriaRole = (
3432
// @ts-expect-error A map should never have any role set
3533
<button role={createSignal('presentation' as const)} />
36-
)
34+
);
3735

3836
// More complex role tests w/ unions
3937

@@ -43,7 +41,6 @@ const aWithHrefInvalid = <a href="foo" role="slider"></a>;
4341

4442
const aWithoutHrefValid = <a role="button"></a>;
4543

46-
4744
const areaWithHrefValid = <area href="foo" role="link"></area>;
4845
// @ts-expect-error An area with an href should not have a role of button
4946
const areaWithHrefInvalid = <area href="foo" role="button"></area>;
@@ -52,15 +49,22 @@ const areaWithoutHrefValid = <area role="button"></area>;
5249
// @ts-expect-error An area with an href should not have a role of button
5350
const areaWithoutHrefInvalid = <area role="slider"></area>;
5451

55-
56-
const imgWithAccessibleNameAriaLabelValid = <img aria-label="foo" role="button" />;
57-
const imgWithAccessibleNameAriaLabelledByValid = <img aria-labelledby="foo" role="button" />;
52+
const imgWithAccessibleNameAriaLabelValid = (
53+
<img aria-label="foo" role="button" />
54+
);
55+
const imgWithAccessibleNameAriaLabelledByValid = (
56+
<img aria-labelledby="foo" role="button" />
57+
);
5858
const imgWithAccessibleNameAltValid = <img alt="foo" role="button" />;
5959
const imgWithAccessibleNameTitleValid = <img title="foo" role="button" />;
60-
// @ts-expect-error An img with an accessible name should not have a role of presentation
61-
const imgWithAccessibleNameAriaLabelInvalid = <img aria-label="foo" role="presentation" />;
62-
// @ts-expect-error An img with an accessible name should not have a role of presentation
63-
const imgWithAccessibleNameAriaLabelledByInvalid = <img aria-labelledby="foo" role="presentation" />;
60+
const imgWithAccessibleNameAriaLabelInvalid = (
61+
// @ts-expect-error An img with an accessible name should not have a role of presentation
62+
<img aria-label="foo" role="presentation" />
63+
);
64+
const imgWithAccessibleNameAriaLabelledByInvalid = (
65+
// @ts-expect-error An img with an accessible name should not have a role of presentation
66+
<img aria-labelledby="foo" role="presentation" />
67+
);
6468
// @ts-expect-error An img with an accessible name should not have a role of presentation
6569
const imgWithAccessibleNameAltInvalid = <img alt="foo" role="presentation" />;
6670
// @ts-expect-error An img with an accessible name should not have a role of presentation
@@ -70,12 +74,13 @@ const imgWithoutAccessibleNameValid = <img role="presentation" />;
7074
// @ts-expect-error An img without an accessible name should not have a role of button
7175
const imgWithoutAccessibleNameInvalid = <img role="button" />;
7276

73-
7477
const inputTypeButtonValid = <input type="button" role="checkbox" />;
7578
// @ts-expect-error An input of type button should not have a role of presentation
7679
const inputTypeButtonInvalid = <input type="button" role="presentation" />;
7780

78-
const inputTypeCheckboxValid = <input type="checkbox" role="menuitemcheckbox" />;
81+
const inputTypeCheckboxValid = (
82+
<input type="checkbox" role="menuitemcheckbox" />
83+
);
7984
// @ts-expect-error An input of type checkbox should not have a role of presentation
8085
const inputTypeCheckboxInvalid = <input type="checkbox" role="presentation" />;
8186

@@ -88,8 +93,10 @@ const inputTypeDateValid = <input type="date" />;
8893
const inputTypeDateInvalid = <input type="date" role="button" />;
8994

9095
const inputTypeDatetimeLocalValid = <input type="datetime-local" />;
91-
// @ts-expect-error An input of type datetime-local should not have a role
92-
const inputTypeDatetimeLocalInvalid = <input type="datetime-local" role="button" />;
96+
const inputTypeDatetimeLocalInvalid = (
97+
// @ts-expect-error An input of type datetime-local should not have a role
98+
<input type="datetime-local" role="button" />
99+
);
93100

94101
const inputTypeEmailValid = <input type="email" role="textbox" />;
95102
// @ts-expect-error An input of type email, without a list attribute, should not have a role of button
@@ -151,11 +158,15 @@ const inputTypeOmittedValid = <input role="combobox" />;
151158
// @ts-expect-error An input of type text should not have a role of presentation
152159
const inputTypeOmittedInvalid = <input role="presentation" />;
153160

154-
const inputTypeEmailListValid = <input type="email" list="foo" role="combobox" />;
161+
const inputTypeEmailListValid = (
162+
<input type="email" list="foo" role="combobox" />
163+
);
155164
// @ts-expect-error An input of type email, with a list attribute, should not have a role of button
156165
const inputTypeEmailListInvalid = <input type="email" role="button" />;
157166

158-
const inputTypeSearchListValid = <input type="search" list="foo" role="combobox" />;
167+
const inputTypeSearchListValid = (
168+
<input type="search" list="foo" role="combobox" />
169+
);
159170
// @ts-expect-error An input of type search, with a list attribute, should not have a role of button
160171
const inputTypeSearchListInvalid = <input type="search" role="button" />;
161172

@@ -167,7 +178,9 @@ const inputTypeTextListValid = <input type="text" list="foo" role="combobox" />;
167178
// @ts-expect-error An input of type text, with a list attribute, should not have a role of button
168179
const inputTypeTextListInvalid = <input type="text" role="button" />;
169180

170-
const inputTypeOmittedListValid = <input type="text" list="foo" role="combobox" />;
181+
const inputTypeOmittedListValid = (
182+
<input type="text" list="foo" role="combobox" />
183+
);
171184
// @ts-expect-error An input of type text, with a list attribute, should not have a role of button
172185
const inputTypeOmittedListInvalid = <input type="text" role="button" />;
173186

@@ -199,8 +212,6 @@ const selectSizeValid = <select size={5} role="listbox" />;
199212
// @ts-expect-error A select with a size other than `0` or `1` should not have a role of menu
200213
const selectSizeInvalid = <select size={5} role="menu" />;
201214

202-
203-
204215
// @ts-expect-error We should correctly type aria attributes like autocomplete
205216
const badAriaValues = <div aria-autocomplete="bad-value" />;
206217
const validAriaValues = <div aria-autocomplete="none" />;

test/ts/hoc-test.tsx renamed to test/ts/hoc.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ComponentConstructor,
66
Component
77
} from '../../';
8-
import { SimpleComponent, SimpleComponentProps } from './Component-test';
8+
import { SimpleComponent, SimpleComponentProps } from './Component.test';
99

1010
export interface highlightedProps {
1111
isHighlighted: boolean;
File renamed without changes.
File renamed without changes.

test/ts/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
"moduleResolution": "node",
66
"lib": ["es6", "dom"],
77
"strict": true,
8-
"typeRoots": ["../../"],
9-
"types": [],
8+
"typeRoots": ["../../", "../../node_modules"],
9+
"types": ["vitest/globals"],
1010
"forceConsistentCasingInFileNames": true,
1111
"jsx": "react",
1212
"jsxFactory": "createElement",
1313
"jsxFragmentFactory": "Fragment",
14+
"skipLibCheck": true,
1415
"paths": {
1516
"preact": ["../../"],
1617
"preact/*": ["../../*"]

vitest.config.mjs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ for (let prop in mangleJson.props.props) {
130130
rename[name] = mangleJson.props.props[prop];
131131
}
132132

133-
const cache = new Map();
134-
const pending = new Map();
135133
export default defineConfig({
136134
resolve: {
137135
alias: rollupAlias,
@@ -211,11 +209,8 @@ export default defineConfig({
211209
}
212210
},
213211
test: {
214-
include: [
215-
'{debug,devtools,hooks,compat,test-utils,jsx-runtime}/test/{browser,shared}/**/*.test.js',
216-
'./test/{browser,shared}/**/*.test.js'
217-
],
218212
cache: false,
213+
globals: true,
219214
coverage: {
220215
enabled: COVERAGE,
221216
include: MINIFY
@@ -242,17 +237,33 @@ export default defineConfig({
242237
reporter: ['html', 'lcovonly', 'text-summary'],
243238
reportsDirectory: './coverage'
244239
},
245-
setupFiles: ['./vitest.setup.js'],
246-
globals: true,
247-
// dangerouslyIgnoreUnhandledErrors: true,
248-
browser: {
249-
// TODO: isolate doesn't work it leaks across all pages
250-
// isolate: false,
251-
provider: 'webdriverio',
252-
enabled: true,
253-
screenshotFailures: false,
254-
headless: true,
255-
instances: [{ browser: 'chrome' }]
256-
}
240+
projects: [
241+
{
242+
extends: true,
243+
test: {
244+
include: ['./test/{shared,node,ts}/**/*.test.js']
245+
}
246+
},
247+
{
248+
extends: true,
249+
test: {
250+
include: [
251+
'{debug,devtools,hooks,compat,test-utils,jsx-runtime}/test/{browser,shared}/**/*.test.js',
252+
'./test/{browser,shared}/**/*.test.js'
253+
],
254+
setupFiles: ['./vitest.setup.js'],
255+
// dangerouslyIgnoreUnhandledErrors: true,
256+
browser: {
257+
// TODO: isolate doesn't work it leaks across all pages
258+
// isolate: false,
259+
provider: 'webdriverio',
260+
enabled: true,
261+
screenshotFailures: false,
262+
headless: true,
263+
instances: [{ browser: 'chrome' }]
264+
}
265+
}
266+
}
267+
]
257268
}
258269
});

0 commit comments

Comments
 (0)