Vitest environment switch: Node built-in resolution boundary when moving from node to jsdom
0 reputation · 05 Apr 2023, 22:17 UTC
Compatibility boundary for Node built-ins across environment changes
Vitest's test.environment option selects the test runtime — node, jsdom, happy-dom, or edge-light — and determines which globals and built-ins are available. Switching from node to a browser-like environment exposes window and document but removes or polyfills Node-specific APIs such as fs, path, and crypto. The configuration also interacts with deps.inline and deps.external because native Node modules may not resolve in jsdom or happy-dom.
Per-file overrides via // @vitest-environment jsdom comments allow mixed environments in one project, yet the documentation does not specify whether dependency resolution respects the per-file environment or follows the project-level setting. This creates uncertainty when a test file imports a module that conditionally uses Node built-ins only under node.
What is the exact resolution boundary for Node built-ins when a per-file environment override differs from the project default? Does deps.inline apply per environment or globally? Can a single dependency graph satisfy both node and jsdom test files without duplicate entry points?