Files
TREK/client/tests/environment/jsdom-native-abort.ts
T
jubnl 0a408c21ac fix(tests): restore native AbortController for undici fetch compatibility
jsdom replaces globalThis.AbortController with its own implementation;
Node.js undici-based fetch validates signals via instanceof against the
native AbortSignal, causing fetch to throw before MSW could intercept.

Fix via custom Vitest environment (tests/environment/jsdom-native-abort.ts)
that captures native AbortController/AbortSignal before jsdom patches them
and restores them after jsdom setup.

Also updates JournalBody test 004 to match component behaviour (headings
rendered as <p>) and removes debug console.log statements.
2026-04-14 15:08:55 +02:00

39 lines
1.5 KiB
TypeScript

/**
* Custom Vitest environment that extends jsdom but preserves the native
* Node.js AbortController and AbortSignal.
*
* Problem: jsdom replaces globalThis.AbortController and AbortSignal with its
* own implementations. Node.js's undici-based fetch validates signals via
* `signal instanceof AbortSignal` against its own native class reference.
* jsdom's AbortSignal instances fail this check, causing fetch to throw:
* TypeError: RequestInit: Expected signal ("AbortSignal {}") to be an
* instance of AbortSignal.
*
* Fix: after jsdom installs its globals, restore the native AbortController
* and AbortSignal so fetch works correctly in tests.
*/
import { builtinEnvironments } from 'vitest/environments';
const jsdomEnv = builtinEnvironments.jsdom;
export default {
name: 'jsdom-native-abort',
transformMode: 'web' as const,
async setup(global: typeof globalThis, options: Record<string, unknown>) {
// Capture native AbortController/AbortSignal BEFORE jsdom patches them
const NativeAbortController = global.AbortController;
const NativeAbortSignal = global.AbortSignal;
// Run standard jsdom setup (installs jsdom globals, including its own AbortController)
const env = await jsdomEnv.setup(global, options as Parameters<typeof jsdomEnv.setup>[1]);
// Restore native AbortController so Node.js fetch (undici) accepts the signals
global.AbortController = NativeAbortController;
global.AbortSignal = NativeAbortSignal;
return env;
},
};