You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When testing a function that has parameters and also utilises import.meta.env, the test will always fail as import.meta.env is undefined. This problem does not exist for functions that take no arguments and they resolve import.meta.env to an object which can be edited via vi.stubEnv.
Reproduction
This problem is very easy to reproduce. All that is needed is two functions, one take takes an argument and returns an object with the argument and some import.meta.env value, and one that simply has the import.meta.env value. This is the simple example I use to demonstrate the issue:
This needs to be tested. Create a file that covers both these functions and for each of them test that when the function is called, it returns an object that contains the correct values. For each of them have 1 attempt that does NOT stub the environment variable and one that does.
A full working example of this can be found on StackBlitz
Vitest replaces all import.meta.env calls with process.env, so it fails in your case because you have your local process variable.
We should probably replace it with __vite_ssr_import_meta__.env that references a proxy to process.env, but has certain rules for boolean values (to fix a separate issue with import.meta.env.DEV being boolean in dev, but the string in test).
This should also make it possible to have dynamic env in the browser (currently changing env doesn't affect other files there).
Describe the bug
When testing a function that has parameters and also utilises import.meta.env, the test will always fail as
import.meta.env
is undefined. This problem does not exist for functions that take no arguments and they resolveimport.meta.env
to an object which can be edited viavi.stubEnv
.Reproduction
This problem is very easy to reproduce. All that is needed is two functions, one take takes an argument and returns an object with the argument and some
import.meta.env
value, and one that simply has theimport.meta.env
value. This is the simple example I use to demonstrate the issue:This needs to be tested. Create a file that covers both these functions and for each of them test that when the function is called, it returns an object that contains the correct values. For each of them have 1 attempt that does NOT stub the environment variable and one that does.
A full working example of this can be found on StackBlitz
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: