[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(browser): fix `import.meta.env` define (#9205)

authored by

Hiroshi Ogawa and committed by
GitHub
(Dec 8, 2025, 9:54 AM +0100) 01a9a58d 01c56454

+21 -1
+3
test/browser/vitest.config.mts
··· 52 52 optimizeDeps: { 53 53 include: ['@vitest/cjs-lib', '@vitest/bundled-lib', 'react/jsx-dev-runtime'], 54 54 }, 55 + define: { 56 + 'import.meta.env.DEFINE_CUSTOM_ENV': JSON.stringify('define-custom-env'), 57 + }, 55 58 test: { 56 59 include: ['test/**.test.{ts,js,tsx}'], 57 60 includeSource: ['src/*.ts'],
+4
test/browser/test/env.test.ts
··· 19 19 expect(import.meta.env.CUSTOM_ENV).toBe('foo') 20 20 }) 21 21 22 + test('import.meta.env via define', () => { 23 + expect(import.meta.env.DEFINE_CUSTOM_ENV).toBe('define-custom-env') 24 + }) 25 + 22 26 test('ignores import.meta.env in string literals', () => { 23 27 expect('import.meta.env').toBe('import' + '.meta.env') 24 28 })
+12
packages/browser/src/node/plugin.ts
··· 622 622 } 623 623 }, 624 624 }, 625 + { 626 + name: 'vitest:browser:__vitest_browser_import_meta_env_init__', 627 + transform: { 628 + handler(code) { 629 + // this transform runs after `vitest:meta-env-replacer` so that 630 + // `import.meta.env` will be handled by Vite import analysis to match behavior. 631 + if (code.includes('__vitest_browser_import_meta_env_init__')) { 632 + return code.replace('__vitest_browser_import_meta_env_init__', 'import.meta.env') 633 + } 634 + }, 635 + }, 636 + }, 625 637 ] 626 638 } 627 639
+2 -1
packages/browser/src/client/tester/tester.ts
··· 101 101 102 102 const state = getWorkerState() 103 103 104 - state.metaEnv = import.meta.env 104 + // @ts-expect-error replaced with `import.meta.env` by transform 105 + state.metaEnv = __vitest_browser_import_meta_env_init__ 105 106 state.onCancel = onCancel 106 107 state.ctx.rpc = rpc as any 107 108 state.rpc = rpc as any