Vitest "import.meta.env is undefined" in CI
Vitest exposes Vite-style env through import.meta.env, but only variables Vite knows about (prefixed, or defined in config) are present. In CI, an unset or unprefixed variable makes import.meta.env.X undefined and code reading it throws.
What this error means
Tests fail with "Cannot read properties of undefined (reading 'VITE_API_URL')" or similar, because import.meta.env lacks the expected key in CI.
TypeError: Cannot read properties of undefined (reading 'VITE_API_URL')
❯ src/api.ts:3:32
3| const base = import.meta.env.VITE_API_URLCommon causes
The env var is not set or not prefixed
Vite only exposes variables with the configured prefix (default VITE_). An unprefixed or unset variable is not on import.meta.env in CI.
No .env file is present in the CI checkout
Local .env files are often gitignored, so the values exist locally but not on the runner.
How to fix it
Provide env values to the test run
- Set the prefixed variables in the workflow env for the test step.
- Or define defaults via
test.envin the config. - Confirm code reads
import.meta.env.VITE_*names that are actually provided.
env:
VITE_API_URL: https://api.example.testDefine test env in config
Set stable defaults so tests do not depend on a gitignored .env file.
export default defineConfig({
test: { env: { VITE_API_URL: 'https://api.example.test' } },
})How to prevent it
- Set required prefixed env vars in the CI job.
- Provide defaults through
test.envfor values not secret. - Do not rely on gitignored .env files in CI.