diff --git a/packages/vitest/src/node/cli-api.ts b/packages/vitest/src/node/cli-api.ts index f9af6ffc5244..b9b83e3573d0 100644 --- a/packages/vitest/src/node/cli-api.ts +++ b/packages/vitest/src/node/cli-api.ts @@ -28,7 +28,7 @@ export async function startVitest( ): Promise { process.env.TEST = 'true' process.env.VITEST = 'true' - process.env.NODE_ENV ??= options.mode || 'test' + process.env.NODE_ENV ??= 'test' if (options.run) options.watch = false diff --git a/packages/vitest/src/node/create.ts b/packages/vitest/src/node/create.ts index 82a8f8f62d81..5ea9e31cdef8 100644 --- a/packages/vitest/src/node/create.ts +++ b/packages/vitest/src/node/create.ts @@ -23,8 +23,8 @@ export async function createVitest(mode: VitestRunMode, options: UserConfig, vit const config: ViteInlineConfig = { logLevel: 'error', configFile: configPath, - // this will make "mode" = "test" inside defineConfig - mode: options.mode || process.env.NODE_ENV || mode, + // this will make "mode": "test" | "benchmark" inside defineConfig + mode: options.mode || mode, plugins: await VitestPlugin(options, ctx), } diff --git a/packages/vitest/src/node/pool.ts b/packages/vitest/src/node/pool.ts index 522209311448..3f13a2c6ebbc 100644 --- a/packages/vitest/src/node/pool.ts +++ b/packages/vitest/src/node/pool.ts @@ -75,7 +75,7 @@ export function createPool(ctx: Vitest): ProcessPool { env: { TEST: 'true', VITEST: 'true', - NODE_ENV: ctx.config.mode || 'test', + NODE_ENV: process.env.NODE_ENV || 'test', VITEST_MODE: ctx.config.watch ? 'WATCH' : 'RUN', ...process.env, ...ctx.config.env, diff --git a/packages/vitest/src/node/workspace.ts b/packages/vitest/src/node/workspace.ts index 5db0d6673436..177eafbf1e5c 100644 --- a/packages/vitest/src/node/workspace.ts +++ b/packages/vitest/src/node/workspace.ts @@ -41,8 +41,8 @@ export async function initializeProject(workspacePath: string | number, ctx: Vit root, logLevel: 'error', configFile, - // this will make "mode" = "test" inside defineConfig - mode: options.mode || ctx.config.mode || process.env.NODE_ENV, + // this will make "mode": "test" | "benchmark" inside defineConfig + mode: options.mode || ctx.config.mode, plugins: [ ...options.plugins || [], WorkspaceVitestPlugin(project, { ...options, root, workspacePath }), diff --git a/test/config/fixtures/mode/example.benchmark.ts b/test/config/fixtures/mode/example.benchmark.ts new file mode 100644 index 000000000000..a216fdfc4165 --- /dev/null +++ b/test/config/fixtures/mode/example.benchmark.ts @@ -0,0 +1,8 @@ +import { bench, describe } from 'vitest' + +describe('example', () => { + bench('simple', () => { + let _ = 0 + _ += 1 + }, { iterations: 1, time: 1, warmupIterations: 0, warmupTime: 0 }) +}) diff --git a/test/config/fixtures/mode/example.test.ts b/test/config/fixtures/mode/example.test.ts new file mode 100644 index 000000000000..5528955569f1 --- /dev/null +++ b/test/config/fixtures/mode/example.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from 'vitest' + +test('should pass', () => { + expect(1).toBe(1) +}) diff --git a/test/config/fixtures/mode/vitest.benchmark.config.ts b/test/config/fixtures/mode/vitest.benchmark.config.ts new file mode 100644 index 000000000000..61158da65a58 --- /dev/null +++ b/test/config/fixtures/mode/vitest.benchmark.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig((env) => { + if (env.mode !== 'benchmark') { + console.error('env.mode: ', env.mode) + throw new Error('env.mode should be equal to "benchmark"') + } + + return ({}) +}) diff --git a/test/config/fixtures/mode/vitest.test.config.ts b/test/config/fixtures/mode/vitest.test.config.ts new file mode 100644 index 000000000000..556dcbc890e1 --- /dev/null +++ b/test/config/fixtures/mode/vitest.test.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig((env) => { + if (env.mode !== 'test') { + console.error('env.mode: ', env.mode) + throw new Error('env.mode should be equal to "test"') + } + + return ({}) +}) diff --git a/test/config/test/mode.test.ts b/test/config/test/mode.test.ts new file mode 100644 index 000000000000..7381e06c6155 --- /dev/null +++ b/test/config/test/mode.test.ts @@ -0,0 +1,23 @@ +import { expect, test } from 'vitest' +import * as testUtils from '../../test-utils' + +test.each([ + { expectedMode: 'test', command: ['run'] }, + { expectedMode: 'benchmark', command: ['bench', '--run'] }, +])(`env.mode should have the $expectedMode value when running in $name mode`, async ({ command, expectedMode }) => { + const { stdout } = await testUtils.runVitestCli(...(command), 'fixtures/mode', '-c', `fixtures/mode/vitest.${expectedMode}.config.ts`) + + expect(stdout).toContain(`✓ fixtures/mode/example.${expectedMode}.ts`) +}) + +test.each([ + { expectedMode: 'test', command: ['bench', '--run'], actualMode: 'benchmark' }, + { expectedMode: 'benchmark', command: ['run'], actualMode: 'test' }, +])(`should return error if actual mode $actualMode is different than expected mode $expectedMode`, async ({ command, expectedMode, actualMode }) => { + const { stdout, stderr } = await testUtils.runVitestCli(...(command), 'fixtures/mode', '-c', `fixtures/mode/vitest.${expectedMode}.config.ts`) + + expect(stderr).toContain(`env.mode: ${actualMode}`) + expect(stderr).toContain('⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯') + expect(stderr).toContain(`Error: env.mode should be equal to "${expectedMode}"`) + expect(stdout).toBe('') +})