Skip to content

Commit

Permalink
fix(vitest): allow rewriting process.env.NODE_MODE when using web tra…
Browse files Browse the repository at this point in the history
…nsform mode (#3957)
  • Loading branch information
sheremet-va authored Aug 15, 2023
1 parent edb322f commit eca4b87
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GlobalSetupPlugin } from './globalSetup'
import { CSSEnablerPlugin } from './cssEnabler'
import { CoverageTransform } from './coverageTransform'
import { MocksPlugin } from './mocks'
import { deleteDefineConfig, resolveOptimizerConfig } from './utils'
import { deleteDefineConfig, hijackVitePluginInject, resolveOptimizerConfig } from './utils'
import { VitestResolver } from './vitestResolver'

export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('test')): Promise<VitePlugin[]> {
Expand Down Expand Up @@ -158,6 +158,8 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
ignored: ['**/*'],
}
}

hijackVitePluginInject(viteConfig)
},
async configureServer(server) {
if (options.watch && process.env.VITE_TEST_WATCHER_DEBUG) {
Expand Down
13 changes: 12 additions & 1 deletion packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { builtinModules } from 'node:module'
import { version as viteVersion } from 'vite'
import type { DepOptimizationOptions, UserConfig as ViteConfig } from 'vite'
import type { DepOptimizationOptions, ResolvedConfig, UserConfig as ViteConfig } from 'vite'
import type { DepsOptimizationOptions, InlineConfig } from '../../types'

export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | undefined, viteOptions: DepOptimizationOptions | undefined, testConfig: InlineConfig) {
Expand Down Expand Up @@ -73,3 +73,14 @@ export function deleteDefineConfig(viteConfig: ViteConfig) {
}
return defines
}

export function hijackVitePluginInject(viteConfig: ResolvedConfig) {
// disable replacing `process.env.NODE_ENV` with static string
const processEnvPlugin = viteConfig.plugins.find(p => p.name === 'vite:client-inject')
if (processEnvPlugin) {
const originalTransform = processEnvPlugin.transform as any
processEnvPlugin.transform = function transform(code, id, options) {
return originalTransform.call(this, code, id, { ...options, ssr: true })
}
}
}
5 changes: 4 additions & 1 deletion packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CSSEnablerPlugin } from './cssEnabler'
import { SsrReplacerPlugin } from './ssrReplacer'
import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mocks'
import { deleteDefineConfig, resolveOptimizerConfig } from './utils'
import { deleteDefineConfig, hijackVitePluginInject, resolveOptimizerConfig } from './utils'
import { VitestResolver } from './vitestResolver'

interface WorkspaceOptions extends UserWorkspaceConfig {
Expand Down Expand Up @@ -100,6 +100,9 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp

return config
},
configResolved(viteConfig) {
hijackVitePluginInject(viteConfig)
},
async configureServer(server) {
try {
const options = deepMerge(
Expand Down
51 changes: 51 additions & 0 deletions test/core/test/env-jsdom.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// @vitest-environment jsdom

import { afterAll, expect, test } from 'vitest'
import { getAuthToken } from '../src/env'

const NODE_ENV = process.env.NODE_ENV

afterAll(() => {
process.env.NODE_ENV = NODE_ENV
})

test('reassigning NODE_ENV', () => {
expect(process.env.NODE_ENV).toBeDefined()
process.env.NODE_ENV = 'development'
expect(process.env.NODE_ENV).toBe('development')
})

test('reads envs from .env file', () => {
expect(import.meta.env.VITE_TEST_ENV).toBe('local')
})

test('can reassign env locally', () => {
import.meta.env.VITEST_ENV = 'TEST'
expect(import.meta.env.VITEST_ENV).toBe('TEST')
})

test('can reassign env everywhere', () => {
import.meta.env.AUTH_TOKEN = '123'
expect(getAuthToken()).toBe('123')
process.env.AUTH_TOKEN = '321'
expect(getAuthToken()).toBe('321')
})

test('can see env in "define"', () => {
expect(import.meta.env.TEST_NAME).toBe('hello world')
expect(process.env.TEST_NAME).toBe('hello world')
})

test('has worker env', () => {
expect(process.env.VITEST_WORKER_ID).toBeDefined()
expect(process.env.VITEST_POOL_ID).toBeDefined()
})

test('custom env', () => {
expect(process.env.CUSTOM_ENV).toBe('foo')
expect(import.meta.env.CUSTOM_ENV).toBe('foo')
})

test('ignores import.meta.env in string literals', () => {
expect('import.meta.env').toBe('import' + '.meta.env')
})

0 comments on commit eca4b87

Please sign in to comment.