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

vi.stubEnv fails to set process.env.NODE_ENV #3933

Closed
6 tasks done
jordykoppen opened this issue Aug 11, 2023 · 0 comments · Fixed by #3957
Closed
6 tasks done

vi.stubEnv fails to set process.env.NODE_ENV #3933

jordykoppen opened this issue Aug 11, 2023 · 0 comments · Fixed by #3957

Comments

@jordykoppen
Copy link

Describe the bug

Since upgrading to version 0.34.1, vi.stubEnv fails to alter the process.env.NODE_ENV variable.

For example:

// process.env.NODE_ENV = "test"

test("should fail", () => {
  vi.stubEnv("NODE_ENV", "production")
    
  // This fails as it returns "test"
  expect(process.env.NODE_ENV).toBe("production")
})

The issue does not persist if I downgrade to 0.33.0.

The latest version also breaks whenever I try to mutate process.env.NODE_ENV directly:

// process.env.NODE_ENV = "test"

test("should fail", () => {
  process.env.NODE_ENV = "production"
    
  expect(process.env.NODE_ENV).toBe("production")
})

The whole test runner fails with the following error:

Error: Parse failure: Assigning to rvalue (15:4)
At file: /Users/USER/workspace/experiments/vitest-stubenv-bug/app/tests/entry.client.test.tsx
Contents of line 15:     "test" = "production";

Reproduction

I've set up a repo to reproduce my issue in the same stack as the application I'm facing problems with (Remix, Cloudflare pages):
https://github.com/jordykoppen/vitest-stubenv-bug

I've included test cases for both issues.

System Info

System:
    OS: macOS 13.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 143.50 MB / 16.00 GB
    Shell: 3.6.0 - /opt/homebrew/bin/fish
  Binaries:
    Node: 18.16.1 - ~/.local/share/bin/node
    Yarn: 1.22.19 - ~/.local/share/bin/yarn
    npm: 9.5.1 - ~/.local/share/bin/npm
    pnpm: 8.6.7 - ~/.local/share/bin/pnpm
  Browsers:
    Chrome: 115.0.5790.170
    Safari: 16.6
  npmPackages:
    @vitejs/plugin-react: 4.0.3 => 4.0.3 
    @vitest/coverage-v8: 0.34.1 => 0.34.1 
    vite: 4.4.7 => 4.4.7 
    vitest: 0.34.1 => 0.34.1

Used Package Manager

pnpm

Validations

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants