vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.15k stars 1.19k forks source link

Vitest is using Node.js native fetch() when environment is Happy DOM #6878

Closed capricorn86 closed 3 weeks ago

capricorn86 commented 3 weeks ago

Describe the bug

It seems like Vitest doesn't populate globalThis with the fetch() implementation from Happy DOM. However, Request and Response are populated.

This causes issues when trying to use a Request or Response object together with fetch().

Additional context Happy DOM plans to make it easier to mock requests and improve cache handling. This will prevent that from working.

Reproduction

describe('test fetch() with Request object', () => {
   it('can use a Request object with fetch()', () => {
        const request = new Request('https://localhost:8080/myFile.json');

        // This will crash with the error "TypeError: Failed to parse URL from [object Request]"
        const response = await fetch(request);

        const json = await response.json();

        expect(json).not.toBeNull();
   });
});

System Info

System:
    OS: Linux 6.11 Ubuntu 24.10 24.10 (Oracular Oriole)
    CPU: (4) x64 Intel(R) Core(TM) i5-7600K CPU @ 3.80GHz
    Memory: 4.04 GB / 14.83 GB
    Container: Yes
    Shell: 5.2.32 - /bin/bash
  Binaries:
    Node: 22.6.0 - ~/.nvm/versions/node/v22.6.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v22.6.0/bin/npm
    bun: 1.1.34 - ~/.bun/bin/bun
  Browsers:
    Chrome: 130.0.6723.58
    Chromium: 130.0.6723.69

Used Package Manager

npm

Validations