Skip to content

Commit

Permalink
fix: fix default import.meta.env.PROD: false (#5561)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Apr 22, 2024
1 parent 80265b4 commit 9c64967
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/vitest/src/runtime/setup-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ function setupDefines(defines: Record<string, any>) {
function setupEnv(env: Record<string, any>) {
if (typeof process === 'undefined')
return
for (const key in env)
// same boolean-to-string assignment as VitestPlugin.configResolved
const { PROD, DEV, ...restEnvs } = env
process.env.PROD = PROD ? '1' : ''
process.env.DEV = DEV ? '1' : ''
for (const key in restEnvs)
process.env[key] = env[key]
}

Expand Down
17 changes: 14 additions & 3 deletions test/core/test/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,20 @@ test('define process and using import.meta.env together', () => {
})

test('PROD, DEV, SSR should be boolean', () => {
expect(typeof import.meta.env.PROD).toEqual('boolean')
expect(typeof import.meta.env.DEV).toEqual('boolean')
expect(typeof import.meta.env.SSR).toEqual('boolean')
expect(import.meta.env.PROD).toBe(false)
expect(import.meta.env.DEV).toBe(true)
expect(process.env.PROD).toBe('')
expect(process.env.DEV).toBe('1')

// see https://github.com/vitest-dev/vitest/issues/5562
if (process.execArgv.includes('--experimental-vm-modules')) {
expect(import.meta.env.SSR).toBe(false)
expect(process.env.SSR).toBe(undefined)
}
else {
expect(import.meta.env.SSR).toBe(true)
expect(process.env.SSR).toBe('1')
}

import.meta.env.SSR = false
expect(import.meta.env.SSR).toEqual(false)
Expand Down

0 comments on commit 9c64967

Please sign in to comment.