Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch is undefined with experimentalVmThreads: true #4199

Closed
6 tasks done
nstepien opened this issue Sep 29, 2023 · 2 comments
Closed
6 tasks done

fetch is undefined with experimentalVmThreads: true #4199

nstepien opened this issue Sep 29, 2023 · 2 comments

Comments

@nstepien
Copy link
Contributor

Describe the bug

Without experimentalVmThreads, our JSDOM tests can use Node's fetch implementation in our tests.
When experimentalVmThreads is enabled, the global fetch is missing.

Reproduction

vite.config.js:

import { defineConfig } from 'vite';

export default defineConfig({
  test: {
    experimentalVmThreads: true,
    environment: 'jsdom'
  }
});

test file:

import { test, expect } from 'vitest';

test('global fetch', () => {
  expect(globalThis.fetch).toBeDefined();
});

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (64) x64 AMD Ryzen Threadripper 3970X 32-Core Processor
    Memory: 23.16 GB / 31.88 GB
  Binaries:
    Node: 20.8.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (117.0.2045.43)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @vitejs/plugin-react: ^4.1.0 => 4.1.0
    @vitest/coverage-v8: ^0.34.5 => 0.34.5
    vite: ^4.4.9 => 4.4.9
    vitest: ^0.34.5 => 0.34.5

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

JSDOM doesn't provide fetch on window object, so it's not there. VM mode is more strict and uses actual context instead of patching global variables, so you should have environment close to what you have in the browser.

I'm not against patching it by extending the context with fetch/Request/Response in https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/integrations/env/jsdom.ts

@nstepien
Copy link
Contributor Author

Yes that'd be nice!
I wonder if there are additional web globals provided by Node that need to be patched in. 🤔

@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants