Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): pass correct mode in vitest config function #4399

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/vitest/src/node/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function startVitest(
): Promise<Vitest | undefined> {
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
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
8 changes: 8 additions & 0 deletions test/config/fixtures/mode/example.benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { bench, describe } from 'vitest'

describe('example', () => {
bench('simple', () => {
let _ = 0
_ += 1
}, { iterations: 1, time: 1, warmupIterations: 0, warmupTime: 0 })
})
5 changes: 5 additions & 0 deletions test/config/fixtures/mode/example.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('should pass', () => {
expect(1).toBe(1)
})
10 changes: 10 additions & 0 deletions test/config/fixtures/mode/vitest.benchmark.config.ts
Original file line number Diff line number Diff line change
@@ -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 ({})
})
10 changes: 10 additions & 0 deletions test/config/fixtures/mode/vitest.test.config.ts
Original file line number Diff line number Diff line change
@@ -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 ({})
})
23 changes: 23 additions & 0 deletions test/config/test/mode.test.ts
Original file line number Diff line number Diff line change
@@ -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('')
})
Loading