Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions vitest.component.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { defineConfig } from 'vitest/config';
import path from 'path';

export default defineConfig({
define: {
'process.env.NODE_ENV': JSON.stringify('test'),
},
test: {
Comment on lines +5 to 8
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using define for process.env.NODE_ENV is a literal string replacement that only affects code using the exact process.env.NODE_ENV syntax. It may fail to cover cases where the environment variable is accessed via destructuring (e.g., const { NODE_ENV } = process.env) or bracket notation (e.g., process.env['NODE_ENV']).

For a more robust solution in Vitest, it is recommended to use the test.env configuration. Alternatively, setting the environment variable in the package.json test script (e.g., NODE_ENV=test vitest ...) ensures it is correctly set for the entire process, including the resolver and all dependencies.

Suggested change
define: {
'process.env.NODE_ENV': JSON.stringify('test'),
},
test: {
test: {
env: {
NODE_ENV: 'test',
},

globals: true,
environment: 'jsdom',
Expand Down
Loading