Skip to content

Commit

Permalink
feat: support custom env in config
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 9, 2022
1 parent cd4bed5 commit 4701e0b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/vitest/src/node/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export async function startVitest(cliFilters: string[], options: CliOptions, vit

process.env.VITEST_MODE = ctx.config.watch ? 'WATCH' : 'RUN'

if (ctx.config.env)
Object.assign(process.env, ctx.config.env)

if (ctx.config.coverage.enabled) {
if (!await ensurePackageInstalled('c8')) {
process.exitCode = 1
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ export interface InlineConfig {
*/
logHeapUsage?: boolean

/**
* Custom environment variables assigned to `process.env` before running tests.
*/
env?: Record<string, string>

/**
* Options for @sinon/fake-timers
*/
Expand Down
5 changes: 5 additions & 0 deletions test/core/test/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ test('can see env in "define"', () => {
test('has worker env', () => {
expect(process.env.VITEST_WORKER_ID).toBeDefined()
})

test('custom env', () => {
expect(process.env.CUSTOM_ENV).toBe('foo')
expect(import.meta.env.CUSTOM_ENV).toBe('foo')
})
3 changes: 3 additions & 0 deletions test/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export default defineConfig({
coverage: {
reporter: ['text', 'html'],
},
env: {
CUSTOM_ENV: 'foo',
},
resolveSnapshotPath: (path, extension) => {
if (path.includes('moved-snapshot'))
return path + extension
Expand Down

0 comments on commit 4701e0b

Please sign in to comment.