-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
Environment variables aren't refreshed when the env file is referred to from test/env in the config file #18993
Comments
This is due to For your example, you don't need |
My use case is specifically around tests for different environments @hi-ogawa. We've 3 different environments, so the default dev/prod of vite doesn't fit well with our project, and we also run against firebase stuff, which relies on keys that you need for doing things locally (like In our setup, we normally have things at least like |
Btw, you probably need Also for this purpose, there is |
I can confirm adding On the loadEnv, yeah, we sometimes have to mix and match stuff and really just want to run the tests in the environment we want to run - specifically in vscode with the Vitest extension. It's one thing having things set in stone for the project, but when trying to actively figure out why something's not right with project x in staging, or trying to figure out how badly does feature y work break when against prod (or something stupider than that), I've constantly found that vite's defaults don't give enough flexibility in that regard. |
It looks like |
Yeah, that's only for tests and I can't think of any use case where that's needed for them. This issue was all about a test setting was changing/changed a behaviour in the non-test environment, all the while the server saying that it detected the change, reloaded the server, and the page, but didn't reflect the change. I get the underlying issues of the mutated env bug, but there nearly shouldn't be any situation where a test configuration should change the behaviour of the vite server. |
Describe the bug
Very simple
.env
Fairly simple
vite.config.ts
and fairly simple check, either
console.log(import.meta.env.VITE_USE_FIREBASE_EMULATORS)
or refer to it inApp.tsx
,On start of server, all's as it should. If I go and remove that env, or change it to say, false, what gets printed and shown? That's right
true
. If I go and completely remove thattest
area from vite config file, what gets printed?true
To fix this, I have to have the test area not refer to my env file at all. Not at startup, not after, at runtime. There's some weird issue where, once that file's "touched", by that area of test env, it's not reprocessed at all.
And, just to be clear, the server does detect the changes,
They just don't bubble up to the code.
Reproduction
https://stackblitz.com/edit/vitejs-vite-pdplp6t1?file=.env,vite.config.mts
Steps to reproduce
Detailed above, refer to your env file in the
test.env
section of your vite config file.System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: