Quantcast
Viewing latest article 27
Browse Latest Browse All 43

Answer by HMR for Jest ReferenceError: Request is not defined

Jest has an outdated jsdom config because it uses outdated jsdom.

I applied the sollution here and it works as long as I add the ts file in the jest config as testEnvironment but won't if I want to use it for a single test since

/** * @jest-environment ./src/test/my-custom-environment */

Isn't doing anything, tried different paths and different files (mjs, cjs, js) but the whole thing is just ignored, even a non existing file won't produce an error.

The solution:

// FixJSDOMEnvironment.tsimport JSDOMEnvironment from 'jest-environment-jsdom';export default class FixJSDOMEnvironment extends JSDOMEnvironment {  constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {    super(...args);    this.global.fetch = fetch;    this.global.Headers = Headers;    this.global.Request = Request;    this.global.Response = Response;  }}// jest.config.jsconst config = {  testEnvironment: './FixJSDOMEnvironment.ts',  ...}

Viewing latest article 27
Browse Latest Browse All 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>