From 9c64967f691fe32de803ae3ddb0f248ea8c3c598 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 22 Apr 2024 16:14:14 +0900 Subject: [PATCH] fix: fix default `import.meta.env.PROD: false` (#5561) --- packages/vitest/src/runtime/setup-common.ts | 6 +++++- test/core/test/env.test.ts | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/runtime/setup-common.ts b/packages/vitest/src/runtime/setup-common.ts index d55cf4ca4aa2..5f5df0eab32a 100644 --- a/packages/vitest/src/runtime/setup-common.ts +++ b/packages/vitest/src/runtime/setup-common.ts @@ -30,7 +30,11 @@ function setupDefines(defines: Record) { function setupEnv(env: Record) { 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] } diff --git a/test/core/test/env.test.ts b/test/core/test/env.test.ts index d5fa3b56d584..71c4eb432b64 100644 --- a/test/core/test/env.test.ts +++ b/test/core/test/env.test.ts @@ -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)