vitest-dev / vitest

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

Using `vitest/importMeta` leaks Node types to web applications #5481

Open amw opened 6 months ago

amw commented 6 months ago

Describe the bug

Projects for web applications need to be careful not to include Node types that could overshadow web browser APIs. For example setTimeout in browser types returns number while in Node it returns Timeout which causes errors like:

error TS2322: Type 'Timeout' is not assignable to type 'number'.

I was able to use Vitest to test my web library, because *.test.ts files were excluded from my tsconfig.json, but I am not able to use import.meta.vitest for inline tests as vitest/importMeta causes the Node types to be added to my project.

Below is link to minimal reproduction, but here's also a screenshot:

vitest-leaking-node-types

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-joykgq?file=src%2Fbasic.ts&view=editor

System Info

System:
    OS: macOS 14.4.1
    CPU: (12) arm64 Apple M2 Pro
    Memory: 1.02 GB / 32.00 GB
    Shell: 3.7.0 - /opt/homebrew/bin/fish
  Binaries:
    Node: 21.6.2 - /opt/homebrew/bin/node
    npm: 10.2.4 - /opt/homebrew/bin/npm
    Watchman: 2024.01.22.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 123.0.6312.105
    Safari: 17.4.1
    Safari Technology Preview: 17.4
  npmPackages:
    vite: latest => 5.2.7 
    vitest: latest => 1.4.0

Used Package Manager

npm

Validations

AriPerkkio commented 5 months ago

Some type declarations are importing types from vite. There's /// <reference types="node" /> in vite/dist/node/index.d.ts.

sheremet-va commented 5 months ago

Some type declarations are importing types from vite. There's /// <reference types="node" /> in vite/dist/node/index.d.ts.

Yep, and there should be no import of vite in importMeta or vitest