Skip to content

Commit 85dcb91

Browse files
MathiasWPclaudeRich-Harris
authored
chore: upgrade to vitest v4 (#18265)
## Summary Bumps `vitest` and `@vitest/coverage-v8` from `^2.1.9` to `^4.1.7` (two major versions). Three small test-harness updates compensate for vitest 4 behavior changes; all 7569 tests still pass. - **`packages/svelte/tests/runtime-browser/test.ts`** — vitest 4 removed the deprecated `describe(name, fn, opts)` signature. Pass options as the second argument. - **`packages/svelte/tests/runtime-legacy/shared.ts`** — vitest 4's jsdom env binds `virtualConsole` to the original `globalThis.console` reference *before* vitest wraps the console, so inline-`<script>` logs and `jsdomError` events no longer reach per-test `console.{log,error}` overrides. Restore vitest 2's behavior by re-routing the virtual console through the live `console` in `beforeAll`. Also promote the window-error listener to a named function and remove it in `finally` — previously leaked listeners from earlier tests kept writing to module-level `unhandled_rejection`, polluting later tests. - **`vitest.config.js`** — bump `testTimeout` to 10s. The 5s default trips a handful of dev-mode tests that exercise `effect_update_depth_exceeded`, whose ~1000 Error-stack captures per flush are slower under vitest 4's deeper async stacks. ## Test plan - [x] `pnpm test` — 7569 passed, 63 skipped, 0 failed --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent a81f965 commit 85dcb91

10 files changed

Lines changed: 319 additions & 743 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ jobs:
1818
strategy:
1919
matrix:
2020
include:
21-
- node-version: 18
21+
# Vitest 4 requires Node 20+, so tests run on 20/22/24. The published
22+
# Svelte package still supports Node >=18 (see packages/svelte/package.json).
23+
- node-version: 20
2224
os: windows-latest
23-
- node-version: 18
25+
- node-version: 20
2426
os: macOS-latest
25-
- node-version: 18
26-
os: ubuntu-latest
2727
- node-version: 20
2828
os: ubuntu-latest
2929
- node-version: 22

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
3333
"@types/node": "^20.11.5",
3434
"@types/picomatch": "^4.0.2",
35-
"@vitest/coverage-v8": "^2.1.9",
35+
"@vitest/coverage-v8": "^4.1.7",
3636
"eslint": "^10.0.0",
3737
"eslint-plugin-lube": "^0.5.1",
3838
"eslint-plugin-svelte": "^3.15.0",
@@ -44,6 +44,6 @@
4444
"typescript": "^5.5.4",
4545
"typescript-eslint": "^8.56.0",
4646
"v8-natives": "^1.2.5",
47-
"vitest": "^2.1.9"
47+
"vitest": "^4.1.7"
4848
}
4949
}

‎packages/svelte/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
"source-map": "^0.7.4",
167167
"tinyglobby": "^0.2.12",
168168
"typescript": "^5.5.4",
169-
"vitest": "^2.1.9",
169+
"vitest": "^4.1.7",
170170
"web-features": "^3.29.0"
171171
},
172172
"dependencies": {

‎packages/svelte/tests/hydration/test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ interface HydrationTest extends BaseTest {
2525
expect_hydration_error?: true;
2626
snapshot?: (target: HTMLElement) => any;
2727
test?: (
28-
assert: typeof import('vitest').assert & {
28+
// `_config.js` test callbacks rely on inferred parameter types, which
29+
// TS treats as non-explicit and rejects for chai 5's assertion-function
30+
// signatures (TS2775). Override the assertion methods we actually use
31+
// with non-assertion equivalents.
32+
assert: Omit<
33+
typeof import('vitest').assert,
34+
'ok' | 'isOk' | 'isTrue' | 'isFalse' | 'exists' | 'notExists' | 'instanceOf'
35+
> & {
36+
ok(value: unknown, message?: string): void;
37+
isOk(value: unknown, message?: string): void;
38+
isTrue(value: unknown, message?: string): void;
39+
isFalse(value: unknown, message?: string): void;
40+
exists(value: unknown, message?: string): void;
41+
notExists(value: unknown, message?: string): void;
42+
instanceOf(value: unknown, type: Function, message?: string): void;
2943
htmlEqual(a: string, b: string, description?: string): void;
3044
},
3145
target: HTMLElement,
@@ -152,7 +166,6 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
152166

153167
if (config.test) {
154168
await config.test(
155-
// @ts-expect-error TS doesn't get it
156169
{
157170
...assert,
158171
htmlEqual: assert_html_equal

‎packages/svelte/tests/runtime-browser/test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const { run: run_browser_tests } = suite_with_variants<
4242

4343
describe.concurrent(
4444
'runtime-browser',
45-
() => run_browser_tests(__dirname),
4645
// Browser tests are brittle and slow on CI
47-
{ timeout: 20000, retry: process.env.CI ? 1 : 0 }
46+
{ timeout: 20000, retry: process.env.CI ? 1 : 0 },
47+
() => run_browser_tests(__dirname)
4848
);
4949

5050
const { run: run_ce_tests } = suite<ReturnType<typeof import('./assert').test>>(
@@ -55,9 +55,9 @@ const { run: run_ce_tests } = suite<ReturnType<typeof import('./assert').test>>(
5555

5656
describe.concurrent(
5757
'custom-elements',
58-
() => run_ce_tests(__dirname, 'custom-elements-samples'),
5958
// Browser tests are brittle and slow on CI
60-
{ timeout: 20000, retry: process.env.CI ? 1 : 0 }
59+
{ timeout: 20000, retry: process.env.CI ? 1 : 0 },
60+
() => run_ce_tests(__dirname, 'custom-elements-samples')
6161
);
6262

6363
async function run_test(

‎packages/svelte/tests/runtime-legacy/shared.ts‎

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,35 @@ import { clear } from '../../src/internal/client/reactivity/batch.js';
1515
import { hydrating } from '../../src/internal/client/dom/hydration.js';
1616
import { ssr_context } from '../../src/internal/server/context.js';
1717

18-
type Assert = typeof import('vitest').assert & {
19-
htmlEqual(a: string, b: string, description?: string): void;
20-
htmlEqualWithOptions(
21-
a: string,
22-
b: string,
23-
opts: {
24-
preserveComments: boolean;
25-
withoutNormalizeHtml: boolean;
26-
},
27-
description?: string
28-
): void;
18+
// `_config.js` files call `assert.ok` etc. with `assert` typed via parameter
19+
// inference, which TypeScript treats as non-explicit. chai 5 (pulled in by
20+
// vitest 4) declares these as assertion functions (`asserts value`), so TS2775
21+
// fires on every call. Override the affected methods with non-assertion
22+
// signatures — the runtime behavior is unchanged.
23+
type NonAssertingMethods = {
24+
ok(value: unknown, message?: string): void;
25+
isOk(value: unknown, message?: string): void;
26+
isTrue(value: unknown, message?: string): void;
27+
isFalse(value: unknown, message?: string): void;
28+
exists(value: unknown, message?: string): void;
29+
notExists(value: unknown, message?: string): void;
30+
instanceOf(value: unknown, type: Function, message?: string): void;
2931
};
3032

33+
type Assert = Omit<typeof import('vitest').assert, keyof NonAssertingMethods> &
34+
NonAssertingMethods & {
35+
htmlEqual(a: string, b: string, description?: string): void;
36+
htmlEqualWithOptions(
37+
a: string,
38+
b: string,
39+
opts: {
40+
preserveComments: boolean;
41+
withoutNormalizeHtml: boolean;
42+
},
43+
description?: string
44+
): void;
45+
};
46+
3147
// TODO remove this shim when we can
3248
// @ts-expect-error
3349
Promise.withResolvers = () => {
@@ -75,6 +91,7 @@ export interface RuntimeTest<Props extends Record<string, any> = Record<string,
7591
raf: {
7692
tick: (ms: number) => void;
7793
};
94+
snapshot: any;
7895
target: HTMLElement;
7996
window: Window & {
8097
Event: typeof Event;
@@ -126,6 +143,16 @@ const listeners = process.rawListeners('unhandledRejection');
126143
beforeAll(() => {
127144
// @ts-expect-error TODO huh?
128145
process.prependListener('unhandledRejection', unhandled_rejection_handler);
146+
147+
// Route inline-`<script>` console calls through `globalThis.console` at
148+
// call time, so per-test `console.{log,warn,error}` overrides see them.
149+
// (jsdom scripts run in a VM with their own `console` object, separate
150+
// from Node's `globalThis.console` — vitest 4's jsdom env no longer
151+
// bridges them per-test.)
152+
const vc = (window as any)._virtualConsole;
153+
for (const m of ['log', 'warn', 'error'] as const) {
154+
vc?.on(m, (...args: any[]) => console[m](...args));
155+
}
129156
});
130157

131158
beforeEach(() => {
@@ -255,6 +282,26 @@ async function run_test_variant(
255282
let warnings: string[] = [];
256283
let errors: string[] = [];
257284
let manual_hydrate = false;
285+
let intercept_errors = false;
286+
287+
// Capture phase so we still see the error if a test installs its own
288+
// `stopImmediatePropagation` listener (e.g. event-handler-*). Named so we
289+
// can remove it in `finally` — otherwise listeners from earlier tests leak
290+
// and keep writing to module-level `unhandled_rejection`. When the test
291+
// intercepts `errors`, mirror jsdom's `Uncaught [...]` format into the
292+
// array — vitest 4's jsdom env no longer routes `jsdomError` to vitest's
293+
// wrapped console reliably (works locally but not in CI's forks pool).
294+
const window_error_listener = (e: ErrorEvent) => {
295+
if (intercept_errors) {
296+
const detail = e.error;
297+
const is_error = detail && detail.name && detail.message !== undefined && detail.stack;
298+
const error_string = is_error ? `[${detail.name}: ${detail.message}]` : String(detail);
299+
errors.push(`Error: Uncaught ${error_string}\n${detail?.stack ?? ''}`, detail);
300+
} else {
301+
unhandled_rejection = e.error;
302+
}
303+
e.preventDefault();
304+
};
258305

259306
{
260307
// use some crude static analysis to determine if logs/warnings are intercepted.
@@ -317,6 +364,7 @@ async function run_test_variant(
317364
}
318365

319366
if (str.slice(0, i).includes('errors') || config.errors) {
367+
intercept_errors = true;
320368
// eslint-disable-next-line no-console
321369
console.error = (...args) => {
322370
errors.push(...args);
@@ -348,10 +396,7 @@ async function run_test_variant(
348396
window.document.head.innerHTML = styles ? `<style>${styles}</style>` : '';
349397
window.document.body.innerHTML = '<main></main>';
350398

351-
window.addEventListener('error', (e) => {
352-
unhandled_rejection = e.error;
353-
e.preventDefault();
354-
});
399+
window.addEventListener('error', window_error_listener, true);
355400

356401
globalThis.requestAnimationFrame = globalThis.setTimeout;
357402

@@ -423,7 +468,6 @@ async function run_test_variant(
423468
await config.test_ssr({
424469
logs,
425470
warnings,
426-
// @ts-expect-error
427471
assert: {
428472
...assert,
429473
htmlEqual: assert_html_equal,
@@ -515,7 +559,6 @@ async function run_test_variant(
515559
}
516560

517561
await config.test({
518-
// @ts-expect-error TS doesn't get it
519562
assert: {
520563
...assert,
521564
htmlEqual: assert_html_equal,
@@ -525,6 +568,7 @@ async function run_test_variant(
525568
component: runes ? props : instance,
526569
instance,
527570
mod,
571+
ok,
528572
target,
529573
snapshot,
530574
window,
@@ -593,6 +637,8 @@ async function run_test_variant(
593637
throw new Error('Hydration state was not cleared');
594638
}
595639

640+
window.removeEventListener('error', window_error_listener, true);
641+
596642
config.after_test?.();
597643

598644
// Free up the microtask queue

‎packages/svelte/tests/sourcemaps/test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ interface SourcemapTest extends BaseTest {
3131
/** The expected `sources` array in the source map */
3232
css_map_sources?: string[];
3333
test?: (obj: {
34-
assert: typeof assert;
34+
// chai 5's `asserts value` signatures trip TS2775 in `_config.js` files
35+
// where `assert` is a destructured parameter (non-explicit). Override
36+
// the assertion methods we use with non-assertion equivalents.
37+
assert: Omit<
38+
typeof assert,
39+
'ok' | 'isOk' | 'isTrue' | 'isFalse' | 'exists' | 'notExists' | 'instanceOf'
40+
> & {
41+
ok(value: unknown, message?: string): void;
42+
isOk(value: unknown, message?: string): void;
43+
isTrue(value: unknown, message?: string): void;
44+
isFalse(value: unknown, message?: string): void;
45+
exists(value: unknown, message?: string): void;
46+
notExists(value: unknown, message?: string): void;
47+
instanceOf(value: unknown, type: Function, message?: string): void;
48+
};
3549
input: string;
3650
map_preprocessed: any;
3751
code_preprocessed: string;

0 commit comments

Comments
 (0)