Skip to content

Commit b8ca387

Browse files
authored
fix(vitest): pass correct mode in vitest config function (#4399)
1 parent 638c4fd commit b8ca387

File tree

9 files changed

+62
-6
lines changed

9 files changed

+62
-6
lines changed

packages/vitest/src/node/cli-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function startVitest(
2828
): Promise<Vitest | undefined> {
2929
process.env.TEST = 'true'
3030
process.env.VITEST = 'true'
31-
process.env.NODE_ENV ??= options.mode || 'test'
31+
process.env.NODE_ENV ??= 'test'
3232

3333
if (options.run)
3434
options.watch = false

packages/vitest/src/node/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export async function createVitest(mode: VitestRunMode, options: UserConfig, vit
2323
const config: ViteInlineConfig = {
2424
logLevel: 'error',
2525
configFile: configPath,
26-
// this will make "mode" = "test" inside defineConfig
27-
mode: options.mode || process.env.NODE_ENV || mode,
26+
// this will make "mode": "test" | "benchmark" inside defineConfig
27+
mode: options.mode || mode,
2828
plugins: await VitestPlugin(options, ctx),
2929
}
3030

packages/vitest/src/node/pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function createPool(ctx: Vitest): ProcessPool {
7575
env: {
7676
TEST: 'true',
7777
VITEST: 'true',
78-
NODE_ENV: ctx.config.mode || 'test',
78+
NODE_ENV: process.env.NODE_ENV || 'test',
7979
VITEST_MODE: ctx.config.watch ? 'WATCH' : 'RUN',
8080
...process.env,
8181
...ctx.config.env,

packages/vitest/src/node/workspace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export async function initializeProject(workspacePath: string | number, ctx: Vit
4141
root,
4242
logLevel: 'error',
4343
configFile,
44-
// this will make "mode" = "test" inside defineConfig
45-
mode: options.mode || ctx.config.mode || process.env.NODE_ENV,
44+
// this will make "mode": "test" | "benchmark" inside defineConfig
45+
mode: options.mode || ctx.config.mode,
4646
plugins: [
4747
...options.plugins || [],
4848
WorkspaceVitestPlugin(project, { ...options, root, workspacePath }),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { bench, describe } from 'vitest'
2+
3+
describe('example', () => {
4+
bench('simple', () => {
5+
let _ = 0
6+
_ += 1
7+
}, { iterations: 1, time: 1, warmupIterations: 0, warmupTime: 0 })
8+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('should pass', () => {
4+
expect(1).toBe(1)
5+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig((env) => {
4+
if (env.mode !== 'benchmark') {
5+
console.error('env.mode: ', env.mode)
6+
throw new Error('env.mode should be equal to "benchmark"')
7+
}
8+
9+
return ({})
10+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig((env) => {
4+
if (env.mode !== 'test') {
5+
console.error('env.mode: ', env.mode)
6+
throw new Error('env.mode should be equal to "test"')
7+
}
8+
9+
return ({})
10+
})

test/config/test/mode.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, test } from 'vitest'
2+
import * as testUtils from '../../test-utils'
3+
4+
test.each([
5+
{ expectedMode: 'test', command: ['run'] },
6+
{ expectedMode: 'benchmark', command: ['bench', '--run'] },
7+
])(`env.mode should have the $expectedMode value when running in $name mode`, async ({ command, expectedMode }) => {
8+
const { stdout } = await testUtils.runVitestCli(...(command), 'fixtures/mode', '-c', `fixtures/mode/vitest.${expectedMode}.config.ts`)
9+
10+
expect(stdout).toContain(`✓ fixtures/mode/example.${expectedMode}.ts`)
11+
})
12+
13+
test.each([
14+
{ expectedMode: 'test', command: ['bench', '--run'], actualMode: 'benchmark' },
15+
{ expectedMode: 'benchmark', command: ['run'], actualMode: 'test' },
16+
])(`should return error if actual mode $actualMode is different than expected mode $expectedMode`, async ({ command, expectedMode, actualMode }) => {
17+
const { stdout, stderr } = await testUtils.runVitestCli(...(command), 'fixtures/mode', '-c', `fixtures/mode/vitest.${expectedMode}.config.ts`)
18+
19+
expect(stderr).toContain(`env.mode: ${actualMode}`)
20+
expect(stderr).toContain('⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯')
21+
expect(stderr).toContain(`Error: env.mode should be equal to "${expectedMode}"`)
22+
expect(stdout).toBe('')
23+
})

0 commit comments

Comments
 (0)