Environment Variables Not Loading During Testing in Convex Application
I'm currently working on adding automated tests to my Convex application. While everything functions correctly in the production environment, I'm encountering an issue during testing: environment variables are not being loaded as expected.
My backend functions rely on environment variables (e.g., API keys, secrets, config values), and these are accessible and work fine when deployed. However, when I run tests using tools like convex-test and Vitest, it seems that the test environment doesn't automatically load or recognize the .env variables.
This is blocking me from properly testing critical parts of the application, especially those dependent on third-party services or configuration.
What I need:
A proper way to load environment variables during testing in a Convex application.
Preferably a solution that works within the Convex runtime context (not the Node.js runtime), since using Node to inject process.env breaks the Convex-specific environment expectations.
Any guidance or best practices for setting up environment-aware tests with Convex would be greatly appreciated.
3 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Vaguely related but I'm not sure how I'm getting this to always evaluate to true even when the env var is not set or set to a different value
❯ npx convex env list
NODE_ENV=development
Update: this works
Which is basically the same logic, so maybe an edge case with NODE_ENV var (set by v8 isolate?)
I set them in
vitest.config.mts
as I don't need actual api keys or secrets for testing, so it looks like this