vitest: [🐞BUG]: `import.meta.env.PROD` has the wrong value, even if stubbed.
Describe the bug
import.meta.env.PROD
will always have the value true.
It will only be false if I stub it to empty string ''
, but that’s definitely not ideal.
Seems there’s an issue when converting the string into a boolean (maybe !!
is used straight away without checking for special value e.g. string 'false'
)
I don’t have enough time to find the regression commit (it’s working as expected before) and the exact logic in the code base, but please get this fixed, thanks🙏
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-ckneff?file=test%2Fbasic.test.ts
System Info
System:
OS: Windows 11 10.0.22631
CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
Memory: 5.85 GB / 31.69 GB
Binaries:
Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
pnpm: 8.6.9 - ~\AppData\Local\pnpm\pnpm.EXE
Browsers:
Edge: Chromium (123.0.2420.81)
Internet Explorer: 11.0.22621.1
npmPackages:
@vitejs/plugin-react-swc: ^3.6.0 => 3.6.0
@vitest/coverage-istanbul: ^1.5.0 => 1.5.0
@vitest/coverage-v8: ^1.5.0 => 1.5.0
@vitest/ui: ^1.5.0 => 1.5.0
vite: ^5.2.8 => 5.2.8
vitest: ^1.5.0 => 1.5.0
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: closed
- Created 3 months ago
- Reactions: 1
- Comments: 15 (15 by maintainers)
Not sure when you last updated, but I think it was always working like this. Under the hood
stubEnv
just setsprocess.env[name] = value
.You can still call
import.meta.env.PROD = false
and it will work correctly.What we should do here is update the type signature and treat
PROD
/DEV
/SSR
differently like we already do inimport.meta.env
and allow booleans just for them:https://github.com/vitest-dev/vitest/blob/82adfa40ac89fad628e5c0f72493e6c905011e67/packages/vite-node/src/utils.ts#L210
This is technically a bug because setting a boolean to
import.meta.env.PROD
directly works correctly: