[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(vite-node): expose all envs from .env file, not just with a prefix `VITE_` (#6017)

authored by

Vladimir and committed by
GitHub
(Jul 2, 2024, 10:44 AM +0200) d87bef96 cf9d84d6

+16 -1
+1
test/vite-node/.env.local
··· 1 + MY_TEST_ENV=hello
+7 -1
packages/vite-node/src/cli.ts
··· 1 1 import { resolve } from 'node:path' 2 2 import cac from 'cac' 3 3 import c from 'picocolors' 4 - import { createServer } from 'vite' 4 + import { createServer, loadEnv } from 'vite' 5 5 import { version } from '../package.json' 6 6 import { ViteNodeServer } from './server' 7 7 import { ViteNodeRunner } from './client' ··· 94 94 plugins: [options.watch && viteNodeHmrPlugin()], 95 95 }) 96 96 await server.pluginContainer.buildStart({}) 97 + 98 + const env = loadEnv(server.config.mode, server.config.envDir, '') 99 + 100 + for (const key in env) { 101 + process.env[key] ??= env[key] 102 + } 97 103 98 104 const node = new ViteNodeServer(server, serverOptions) 99 105
+2
test/vite-node/src/cli-print-env.js
··· 1 + // eslint-disable-next-line no-console 2 + console.log(JSON.stringify(process.env, null, 2))
+6
test/vite-node/test/cli.test.ts
··· 39 39 expect(parseResult(cli1.stdout)).include('--version').include('--help') 40 40 }) 41 41 42 + it('exposes .env variables', async () => { 43 + const { stdout } = await runViteNodeCli(resolve(__dirname, '../src/cli-print-env.js')) 44 + const env = JSON.parse(stdout) 45 + expect(env.MY_TEST_ENV).toBe('hello') 46 + }) 47 + 42 48 it.each(['index.js', 'index.cjs', 'index.mjs'])('correctly runs --watch %s', async (file) => { 43 49 const entryPath = resolve(__dirname, '../src/watch', file) 44 50 const { viteNode } = await runViteNodeCli('--watch', entryPath)