Skip to content

Commit

Permalink
test(vitest): add mode in config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Oct 30, 2023
1 parent b1ebcb3 commit 51b3c43
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/config/fixtures/mode/example.bench.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 ({})
})
22 changes: 22 additions & 0 deletions test/config/test/mode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, test } from 'vitest'
import * as testUtils from '../../test-utils'

describe.each([
{ mode: 'test', name: 'test' },
{ mode: 'bench', name: 'benchmark' },
] as const)('checking mode: $mode', ({ mode, name }) => {
test(`env.mode should have the ${mode} value when running in ${name} mode`, async () => {
const { stdout } = await testUtils.runVitestCli(...(mode === 'test' ? ['run'] : ['bench', '--run']), 'fixtures/mode', '-c', `fixtures/mode/vitest.${name}.config.ts`)

expect(stdout).toContain(`✓ fixtures/mode/example.${mode}.ts`)
})

test(`should return error when running vitest ${mode === 'test' ? 'bench' : 'run'}`, async () => {
const { stdout, stderr } = await testUtils.runVitestCli(...(mode === 'bench' ? ['run'] : ['bench', '--run']), 'fixtures/mode', '-c', `fixtures/mode/vitest.${name}.config.ts`)

expect(stderr).toContain(`env.mode: ${name === 'test' ? 'benchmark' : 'test'}`)
expect(stderr).toContain('⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯')
expect(stderr).toContain(`Error: env.mode should be equal to "${name}"`)
expect(stdout).toBe('')
})
})

0 comments on commit 51b3c43

Please sign in to comment.