Wisdom woke
Wisdom woke3w ago

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
Convex Bot
Convex Bot3w ago
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!
sleepless
sleepless3w ago
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
const isProduction = process.env.NODE_ENV === "production";

export const sendTestEmail = internalMutation({
handler: async (ctx) => {
console.log("Is production:", isProduction);
...
},
});

// when I run it I always get:
log 'Is production:' true
const isProduction = process.env.NODE_ENV === "production";

export const sendTestEmail = internalMutation({
handler: async (ctx) => {
console.log("Is production:", isProduction);
...
},
});

// when I run it I always get:
log 'Is production:' true
❯ npx convex env list NODE_ENV=development Update: this works
const IS_PROD_ENV = process.env.IS_PROD_ENV === "true";
const IS_PROD_ENV = process.env.IS_PROD_ENV === "true";
Which is basically the same logic, so maybe an edge case with NODE_ENV var (set by v8 isolate?)
erquhart
erquhart2w ago
I set them in vitest.config.mts as I don't need actual api keys or secrets for testing, so it looks like this
import path from 'node:path'
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
environment: 'edge-runtime',
server: { deps: { inline: ['convex-test'] } },
include: ['convex/**/*.test.ts'],
env: {
SOME_VAR: 'test',
SOME_KEY: 'testtesttesttesttesttesttesttest',
...moreKeys,
},
},
resolve: {
alias: {
'~': path.resolve(__dirname, '.'),
'@cvx': path.resolve(__dirname, './convex'),
},
},
})
import path from 'node:path'
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
environment: 'edge-runtime',
server: { deps: { inline: ['convex-test'] } },
include: ['convex/**/*.test.ts'],
env: {
SOME_VAR: 'test',
SOME_KEY: 'testtesttesttesttesttesttesttest',
...moreKeys,
},
},
resolve: {
alias: {
'~': path.resolve(__dirname, '.'),
'@cvx': path.resolve(__dirname, './convex'),
},
},
})

Did you find this page helpful?