Skip to content

vitest 4 broke stubEnv(..., undefined) to unset an environment variable #10525

Open

Description

@DamienCassou

Describe the bug

According to:

I would expect

vi.stubEnv("foo", undefined);

to delete process.env.foo.

This was working fine until vitest 4: now, passing undefined as value to stubEnv() doesn't seem to do anything anymore.

Reproduction

In this PR of a dedicated repo, you will see that the same test:

  • passes in vitest 3
  • fails in vitest 4
  • fails in vitest 5-beta

The test fails in vitest 4 and 5-beta because the initial value of the environment variable remains after the call to stubEnv(..., undefined)

System Info

System:
    OS: Linux 6.17 Ubuntu 24.04.4 LTS 24.04.4 LTS (Noble Numbat)
    CPU: (4) x64 AMD EPYC 7763 64-Core Processor
    Memory: 14.62 GB / 15.61 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 24.16.0 - /opt/hostedtoolcache/node/24.16.0/x64/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 11.13.0 - /opt/hostedtoolcache/node/24.16.0/x64/bin/npm
  Browsers:
    Chrome: 148.0.7778.178
    Chromium: 148.0.7778.0
    Firefox: 151.0.1
    Firefox Developer Edition: 151.0.1
  npmPackages:
    vitest: ^4.1.8 => 4.1.8

Used Package Manager

npm

Validations

Activity

  1. added theissue type on Jun 5, 2026
  2. clanzhang commented on Jun 14, 2026

    @clanzhang

    I'd like to work on this. PR soon.

  3. DamienCassou commented on Jul 1, 2026

    @DamienCassou
    Author

    @clanzhang did you find anything?

  4. clanzhang commented on Jul 1, 2026

    @clanzhang

    @clanzhang did you find anything?

    Sorry for the delay! I've identified the root cause — the metaEnv Proxy in worker.ts has get/set traps that use the live process.env, but is missing a deleteProperty trap. When user code replaces process.env (e.g. process.env = {}), the delete in stubEnv(key, undefined) operates on the stale Proxy target instead of the current process.env.

    The fix is to add a deleteProperty trap. Opening a PR now.

  5. DamienCassou commented on Jul 29, 2026

    @DamienCassou
    Author

    @clanzhang Have you opened a PR?

  6. clanzhang commented on Aug 3, 2026

    @clanzhang

    @clanzhang Have you opened a PR?

    Sorry, I don't really want to submit this PR — seeing that person gives me PTSD. Please don't rush me into submitting the PR either. Thanks.

  7. Prajwal2246 commented on Oct 7, 2026

    @Prajwal2246
    Contributor

    Still happens on 5.0.3 (node 24.17.0) with the repro. Passes on 3.2.4, fails on 4.0.18 and later.

    It only fails when process.env is reassigned (process.env = {}). Without that, stubEnv(key, undefined) works.

    I tested adding a deleteProperty trap to the import.meta.env proxy in the installed vitest and the repro passed, so the cause clanzhang mentioned looks right. #10539 tried the same fix but was auto-closed. Would you accept a PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions