Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix default import.meta.env.PROD: false #5561

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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