I am trying to test a file that relies on environment variables, though it doesn't load them. I use vite+vitest with React.js. It works well on development/production, but when I test the component with the variable is undefined: import.meta.env.VITE_API_URL
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
console.log('mode', mode, loadEnv(mode, process.cwd())) <----- HERE IT APPEARS TO BE DEFINED WHEN THE TEST STARTS
return defineConfig({
base: '/',
server: {
port: 3000,
proxy: {
'/public': {
target: `${process.env.VITE_API_URL}/`,
changeOrigin: true,
},
},
},
publicDir: './public',
plugins: [react()],
test: {
globals: true,
environment: 'jsdom',
setupFiles: './src/__test__/setup.js',
include: ['./src/__test__/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
})
}
Describe the bug
I am trying to test a file that relies on environment variables, though it doesn't load them. I use vite+vitest with React.js. It works well on development/production, but when I test the component with the variable is undefined: import.meta.env.VITE_API_URL
When I initialize the test, it loads properly on my vite.config.js:
Reproduction
Github repo: https://github.com/IT-Academy-BCN/ita-directory
Branch: https://github.com/IT-Academy-BCN/ita-directory/tree/511-test-recover-password
File: https://github.com/IT-Academy-BCN/ita-directory/blob/511-test-recover-password/frontend/src/__test__/pages/UserFlow/RecoverPassword.test.jsx
System Info
envinfo command not foundUsed Package Manager
npm
Validations