-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[🐞BUG]: import.meta.env.PROD
has the wrong value, even if stubbed.
#5525
Comments
import.meta.env.PROD
has the wrong value, even if stubbed.import.meta.env.PROD
has the wrong value, even if stubbed.
I'm aware that in the environment variable it's always with type |
This is technically a bug because setting a boolean to import.meta.env.PROD = true |
It looks like the default value of https://stackblitz.com/edit/vitest-dev-vitest-vbi9xu?file=test%2Fbasic.test.ts Show sample codeimport { test, expect } from "vitest";
test("repro", () => {
// v1.4.0
[
false,
true,
true,
"",
"1",
"1"
];
// v1.5.0
[
true,
true,
true,
"false",
"true",
"1"
];
console.log([
import.meta.env.PROD,
import.meta.env.DEV,
import.meta.env.SSR,
process.env.PROD,
process.env.DEV,
process.env.SSR,
]);
});
I'm not sure if anything changed on the stubbing behavior (namely assigning Btw, what do you mean by "Vite's behavior"? In Vite, |
I might be misleading, but what I mean is Vite seems correctly treating |
I still fail to understand what/when Vite is "treating" anything in terms of |
Sorry, maybe just forget that, the ultimate goal of this issue is to make sure Vitest treats |
Vitest currently handles this boolean/string problem by making vitest/packages/vitest/src/node/plugins/index.ts Lines 166 to 169 in 643bed6
If there is something Vite already does, then Vitest aligning with it would totally makes sense, so that's why I was curious about that. Otherwise, current recommendation would be to stick with our internal representation, namely using an empty string |
Yeah stubbing to empty string will definitely work, but that doesn't make sense. Imagine you are setting your env by cli, for example using |
|
Yeah that makes sense, and I'm absolutely fine to change my test file to |
Not sure when you last updated, but I think it was always working like this. Under the hood You can still call What we should do here is update the type signature and treat vitest/packages/vite-node/src/utils.ts Line 210 in 82adfa4
|
I tried with the exact vitest and vite version when that code is working in my codebase, but no luck, it's still breaking. No idea what's happening. |
Actually, I got what I was doing before. |
In other word, if my memory is correct, |
Yeah, this matches with my reproduction in #5525 (comment) This started since v1.5.0 and it's likely a bug, so I made a PR to fix it #5561. |
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
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: