Stub Environment Variables in Vitest
You can stub environment variables in Vitest using vi.stubEnv
. This will stub the value on process.env
and import.meta.env
.
You can also reset all env vars back to their original value with vi.unstubAllEnvs
.
process.env.COOL_ENV_VAR; // => "test"
import.meta.env.COOL_ENV_VAR; // => "test"
vi.stubEnv('COOL_ENV_VAR', "stubbed");
process.env.COOL_ENV_VAR; // => "stubbed"
import.meta.env.COOL_ENV_VAR; // => "stubbed"
vi.unstubAllEnvs();
process.env.COOL_ENV_VAR; // => "test"
import.meta.env.COOL_ENV_VAR; // => "test"
Tweet