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

Functions with parameters cant find import.meta.env #3735

Closed
6 tasks done
Omnicpie opened this issue Jul 4, 2023 · 2 comments · Fixed by #3944
Closed
6 tasks done

Functions with parameters cant find import.meta.env #3735

Omnicpie opened this issue Jul 4, 2023 · 2 comments · Fixed by #3944

Comments

@Omnicpie
Copy link

Omnicpie commented Jul 4, 2023

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 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:

export const getDefaults = () => ({ process: '', name: 'some name', mode: import.meta.env.MODE });

export const getDefaultsWithParam = (process: string) => ({
  process,
  name: 'some name',
  mode: import.meta.env.MODE,
});

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

System:
    OS: macOS 13.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 462.12 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.11.0 - ~/.nvm/versions/node/v18.11.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.11.0/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.11.0/bin/npm
  Browsers:
    Chrome: 114.0.5735.198
    Edge: 114.0.1823.51
    Firefox: 114.0.2
    Firefox Developer Edition: 100.0
    Safari: 16.5
  npmPackages:
    @vitejs/plugin-react: ^3.1.0 => 3.1.0 
    @vitest/coverage-c8: ^0.24.5 => 0.24.5 
    @vitest/coverage-v8: ^0.32.0 => 0.32.0 
    @vitest/ui: latest => 0.32.0 
    vite: ^4.2.1 => 4.2.2 
    vitest: ^0.32.0 => 0.32.0

Used Package Manager

yarn

Validations

@stackblitz
Copy link

stackblitz bot commented Jul 4, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sheremet-va
Copy link
Member

sheremet-va commented Jul 4, 2023

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).

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