/** * Custom Jest environment that extends jest-environment-jsdom and polyfills * the Fetch API (fetch, Request, Response, Headers) from Node 18+'s native * implementation. This lets tests spy on `global.fetch` with jest.spyOn. */ const JSDOMEnvironment = require('jest-environment-jsdom').default; class CustomJSDOMEnvironment extends JSDOMEnvironment { async setup() { await super.setup(); // Node 18+ exposes fetch on globalThis; copy it into the jsdom sandbox. if (typeof globalThis.fetch === 'function') { this.global.fetch = globalThis.fetch.bind(globalThis); this.global.Request = globalThis.Request; this.global.Response = globalThis.Response; this.global.Headers = globalThis.Headers; } } } module.exports = CustomJSDOMEnvironment;