Skip to content

[Bug]: Fetch API unavailable in "jsdom" test environment #13834

@hahanein

Description

@hahanein

Version

29.4.1

Steps to reproduce

  1. Clone my repo at https://github.com/hahanein/no-fetch-api-in-jsdom-environment
  2. pnpm i
  3. pnpm test
  4. You should see the error "ReferenceError: fetch is not defined"

Expected behavior

I expect to see the following results:

 PASS  ./script.test.js
  ✓ testing fetch (670 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total

Actual behavior

The test is failing with the error message "ReferenceError: fetch is not defined".

However, when you remove the line testEnvironment: 'jsdom' from jest.config.js everything is working as expected.

Additional context

Due to this behavior we are forced to polyfill the Fetch API (fetch, Response, Request, ...) at my workplace. This is becoming progressively more tedious. node-fetch/node-fetch#1263

Environment

System:
    OS: Linux 6.1 Arch Linux
    CPU: (8) x64 AMD Ryzen 5 PRO 3500U w/ Radeon Vega Mobile Gfx
  Binaries:
    Node: 19.4.0 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.19.2 - /usr/bin/npm
  npmPackages:
    jest: ^29.4.1 => 29.4.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions