diff --git a/test/benchmark/test/basic.test.ts b/test/benchmark/test/basic.test.ts index 86b4eee0a..cbe91e363 100644 --- a/test/benchmark/test/basic.test.ts +++ b/test/benchmark/test/basic.test.ts @@ -16,7 +16,7 @@ it('basic', { timeout: 60_000 }, async () => { // Verify that type testing cannot be used with benchmark typecheck: { enabled: true }, - }, [], 'benchmark') + }, [], { mode: 'benchmark' }) expect(result.stderr).toBe('') expect(result.exitCode).toBe(0) diff --git a/test/benchmark/test/compare.test.ts b/test/benchmark/test/compare.test.ts index 32bff227f..102a5a972 100644 --- a/test/benchmark/test/compare.test.ts +++ b/test/benchmark/test/compare.test.ts @@ -11,7 +11,7 @@ test('compare', { timeout: 60_000 }, async () => { root: './fixtures/compare', outputJson: './bench.json', reporters: ['default'], - }, [], 'benchmark') + }, [], { mode: 'benchmark' }) expect(result.exitCode).toBe(0) expect(fs.existsSync('./fixtures/compare/bench.json')).toBe(true) } @@ -22,7 +22,7 @@ test('compare', { timeout: 60_000 }, async () => { root: './fixtures/compare', compare: './bench.json', reporters: ['default'], - }, [], 'benchmark') + }, [], { mode: 'benchmark' }) expect(result.exitCode).toBe(0) const lines = result.stdout.split('\n').slice(4).slice(0, 6) const expected = ` diff --git a/test/benchmark/test/reporter.test.ts b/test/benchmark/test/reporter.test.ts index f5c89d52b..566efa98e 100644 --- a/test/benchmark/test/reporter.test.ts +++ b/test/benchmark/test/reporter.test.ts @@ -4,7 +4,7 @@ import { runVitest } from '../../test-utils' it('summary', async () => { const root = pathe.join(import.meta.dirname, '../fixtures/reporter') - const result = await runVitest({ root }, ['summary.bench.ts'], 'benchmark') + const result = await runVitest({ root }, ['summary.bench.ts'], { mode: 'benchmark' }) expect(result.stderr).toBe('') expect(result.stdout).not.toContain('NaNx') expect(result.stdout.split('BENCH Summary')[1].replaceAll(/[0-9.]+x/g, '(?)')).toMatchSnapshot() @@ -12,7 +12,7 @@ it('summary', async () => { it('non-tty', async () => { const root = pathe.join(import.meta.dirname, '../fixtures/basic') - const result = await runVitest({ root }, ['base.bench.ts'], 'benchmark') + const result = await runVitest({ root }, ['base.bench.ts'], { mode: 'benchmark' }) const lines = result.stdout.split('\n').slice(4).slice(0, 11) const expected = `\ ✓ base.bench.ts > sort @@ -40,7 +40,7 @@ it.for([true, false])('includeSamples %s', async (includeSamples) => { benchmark: { includeSamples }, }, ['summary.bench.ts'], - 'benchmark', + { mode: 'benchmark' }, ) assert(result.ctx) const allSamples = [...result.ctx.state.idMap.values()] diff --git a/test/benchmark/test/sequential.test.ts b/test/benchmark/test/sequential.test.ts index b40db7e09..2aea431d8 100644 --- a/test/benchmark/test/sequential.test.ts +++ b/test/benchmark/test/sequential.test.ts @@ -5,7 +5,7 @@ import { runVitest } from '../../test-utils' it('sequential', async () => { const root = pathe.join(import.meta.dirname, '../fixtures/sequential') - await runVitest({ root }, [], 'benchmark') + await runVitest({ root }, [], { mode: 'benchmark' }) const testLog = await fs.promises.readFile(pathe.join(root, 'test.log'), 'utf-8') expect(testLog).toMatchSnapshot() }) diff --git a/test/browser/specs/benchmark.test.ts b/test/browser/specs/benchmark.test.ts index ed3d7dd2e..e7e5eb608 100644 --- a/test/browser/specs/benchmark.test.ts +++ b/test/browser/specs/benchmark.test.ts @@ -4,7 +4,7 @@ import { runVitest } from '../../test-utils' const IS_PLAYWRIGHT = process.env.PROVIDER === 'playwright' test('benchmark', async () => { - const result = await runVitest({ root: 'fixtures/benchmark' }, [], 'benchmark') + const result = await runVitest({ root: 'fixtures/benchmark' }, [], { mode: 'benchmark' }) expect(result.stderr).toReportNoErrors() if (IS_PLAYWRIGHT) { diff --git a/test/cli/test/annotations.test.ts b/test/cli/test/annotations.test.ts index 05dac499e..9020fe2ed 100644 --- a/test/cli/test/annotations.test.ts +++ b/test/cli/test/annotations.test.ts @@ -58,6 +58,7 @@ describe('API', () => { 'basic.test.ts': annotationTest, 'test-3.js': test3Content, 'test-4.js': '', + 'vitest.config.js': { test: options }, }, { includeTaskLocation: true, @@ -97,10 +98,6 @@ describe('API', () => { }, ], }, - {}, - { - test: options, - }, ) expect(stderr).toBe('') diff --git a/test/cli/test/artifacts.test.ts b/test/cli/test/artifacts.test.ts index a7ab8c174..37c7550d1 100644 --- a/test/cli/test/artifacts.test.ts +++ b/test/cli/test/artifacts.test.ts @@ -58,6 +58,7 @@ describe('API', () => { 'test-4.js': '', }, { + ...options, includeTaskLocation: true, reporters: [ 'default', @@ -100,10 +101,6 @@ describe('API', () => { }, ], }, - {}, - { - test: options, - }, ) expect(stderr).toBe('') diff --git a/test/cli/test/fails.test.ts b/test/cli/test/fails.test.ts index f2f7c9a3e..d966cdfaa 100644 --- a/test/cli/test/fails.test.ts +++ b/test/cli/test/fails.test.ts @@ -116,14 +116,12 @@ it('prints a warning if the assertion is not awaited in the browser mode', async expect(Promise.resolve(1)).resolves.toBe(1) }) `, - }, {}, {}, { - test: { - browser: { - enabled: true, - instances: [{ browser: 'chromium' }], - provider: playwright(), - headless: true, - }, + }, { + browser: { + enabled: true, + instances: [{ browser: 'chromium' }], + provider: playwright(), + headless: true, }, }) expect(stderr).toContain('Promise returned by \`expect(actual).resolves.toBe(expected)\` was not awaited') diff --git a/test/cli/test/group-order.test.ts b/test/cli/test/group-order.test.ts index 4d5c1d033..52e8baaa3 100644 --- a/test/cli/test/group-order.test.ts +++ b/test/cli/test/group-order.test.ts @@ -8,7 +8,7 @@ test('tests run according to the group order', async () => { 'example.2-2.test.ts': `test('2-2', () => {})`, 'example.3.test.ts': `test('3', () => {})`, }, { - globals: true, + $cliOptions: { globals: true }, // run projects in the opposite order! projects: [ { diff --git a/test/cli/test/open-telemetry.test.ts b/test/cli/test/open-telemetry.test.ts index 9a7448202..2ac096814 100644 --- a/test/cli/test/open-telemetry.test.ts +++ b/test/cli/test/open-telemetry.test.ts @@ -48,8 +48,7 @@ describe.for([ // root needs to be set before vitest sets up, // but browser options need to be in the config already root: './fixtures/otel-tests', - }, [], 'test', { - test: (type === 'root' + ...(type === 'root' ? config : { projects: [{ test: config }] }), }) diff --git a/test/cli/test/shared-env.test.ts b/test/cli/test/shared-env.test.ts index fa5eda537..ea466174f 100644 --- a/test/cli/test/shared-env.test.ts +++ b/test/cli/test/shared-env.test.ts @@ -24,7 +24,10 @@ test('document is the same', () => { isolate: false, }, }, - }, { sequence: { sequencer: StableTestFileOrderSorter }, maxWorkers }) + }, { + sequence: { sequencer: StableTestFileOrderSorter }, + maxWorkers, + }) expect(stderr).toBe('') }) diff --git a/test/config/test/bail.test.ts b/test/config/test/bail.test.ts index abd413ec9..ae6f47d66 100644 --- a/test/config/test/bail.test.ts +++ b/test/config/test/bail.test.ts @@ -69,14 +69,11 @@ for (const config of configs) { const { exitCode, stdout, ctx } = await runVitest({ root: './fixtures/bail', bail: 1, - }, [], 'test', { - test: { - ...config, - env: { - THREADS: process.env.THREADS, - }, + ...config, + env: { + THREADS: process.env.THREADS, }, - }) + }, []) expect(ctx?.config.pool).toBe(config.pool || 'forks') expect(ctx?.config.browser.enabled).toBe(config.browser?.enabled ?? false) diff --git a/test/config/test/browser-html.test.ts b/test/config/test/browser-html.test.ts index 93f31b856..38b3ef4c7 100644 --- a/test/config/test/browser-html.test.ts +++ b/test/config/test/browser-html.test.ts @@ -8,7 +8,7 @@ test('throws an error with non-existing path', async () => { const { stderr } = await runVitest({ root, config: './vitest.config.non-existing.ts', - }, [], 'test', {}, { fails: true }) + }, [], { fails: true }) expect(stderr).toContain(`Tester HTML file "${resolve(root, './some-non-existing-path')}" doesn't exist.`) }) diff --git a/test/config/test/cache.test.ts b/test/config/test/cache.test.ts index 31503ef75..c6c3d0202 100644 --- a/test/config/test/cache.test.ts +++ b/test/config/test/cache.test.ts @@ -43,10 +43,10 @@ test('use cacheDir', async () => { { root, include: ['*.test.ts'], + $viteConfig: { + cacheDir: 'node_modules/.vite-custom', + }, }, - [], - 'test', - { cacheDir: 'node_modules/.vite-custom' }, ) expect(stdout).toContain('✓ basic.test.ts >') @@ -132,10 +132,10 @@ describe('with optimizer enabled', () => { root, include: ['*.test.ts'], deps, + $viteConfig: { + cacheDir: 'node_modules/.vite-custom', + }, }, - [], - 'test', - { cacheDir: 'node_modules/.vite-custom' }, ) expect(stdout).toContain('✓ basic.test.ts >') diff --git a/test/config/test/cli-config.test.ts b/test/config/test/cli-config.test.ts index e719da85d..0269f0954 100644 --- a/test/config/test/cli-config.test.ts +++ b/test/config/test/cli-config.test.ts @@ -16,20 +16,22 @@ test('can pass down the config as a module', async () => { it('correctly inherit from the cli', async () => { const { ctx } = await runVitest({ - root: 'fixtures/workspace-flags', - logHeapUsage: true, - allowOnly: true, - sequence: { - seed: 123, + $cliOptions: { + root: 'fixtures/workspace-flags', + logHeapUsage: true, + allowOnly: true, + sequence: { + seed: 123, + }, + testTimeout: 5321, + pool: 'forks', + globals: true, + expandSnapshotDiff: true, + retry: 6, + testNamePattern: 'math', + passWithNoTests: true, + bail: 100, }, - testTimeout: 5321, - pool: 'forks', - globals: true, - expandSnapshotDiff: true, - retry: 6, - testNamePattern: 'math', - passWithNoTests: true, - bail: 100, }) const project = ctx!.projects[0] const config = project.config diff --git a/test/config/test/conditions-cli.test.ts b/test/config/test/conditions-cli.test.ts index be959e59a..c04c436d2 100644 --- a/test/config/test/conditions-cli.test.ts +++ b/test/config/test/conditions-cli.test.ts @@ -10,9 +10,10 @@ test('correctly imports external dependencies with a development condition', asy external: [/conditions-pkg/], }, }, - }, [], 'test', { - define: { - TEST_CONDITION: '"development"', + $viteConfig: { + define: { + TEST_CONDITION: '"development"', + }, }, }) @@ -30,9 +31,10 @@ test('correctly imports external dependencies with a production condition', asyn external: [/conditions-pkg/], }, }, - }, [], 'test', { - define: { - TEST_CONDITION: '"production"', + $viteConfig: { + define: { + TEST_CONDITION: '"production"', + }, }, }) @@ -49,17 +51,18 @@ test('correctly imports external dependencies with a custom condition', async () external: [/conditions-pkg/], }, }, - }, [], 'test', { - resolve: { - conditions: ['custom'], - }, - ssr: { + $viteConfig: { resolve: { conditions: ['custom'], }, - }, - define: { - TEST_CONDITION: '"custom"', + ssr: { + resolve: { + conditions: ['custom'], + }, + }, + define: { + TEST_CONDITION: '"custom"', + }, }, }) diff --git a/test/config/test/console-color.test.ts b/test/config/test/console-color.test.ts index 8067756ac..b552908a1 100644 --- a/test/config/test/console-color.test.ts +++ b/test/config/test/console-color.test.ts @@ -10,7 +10,7 @@ test('with color', async () => { NO_COLOR: undefined, GITHUB_ACTIONS: undefined, }, - }, undefined, undefined, undefined, { preserveAnsi: true }) + }, [], { preserveAnsi: true }) expect(stdout).toContain('\x1B[33mtrue\x1B[39m\n') }) @@ -24,7 +24,7 @@ test('without color', async () => { NO_COLOR: '1', GITHUB_ACTIONS: undefined, }, - }, undefined, undefined, undefined, { preserveAnsi: true }) + }, [], { preserveAnsi: true }) expect(stdout).toContain('true\n') expect(stdout).not.toContain('\x1B[33mtrue\x1B[39m\n') @@ -44,7 +44,7 @@ test.skipIf(process.platform === 'win32')('without color, forks pool in non-TTY // FORCE_TTY=false will make the check `false` FORCE_TTY: 'false', }, - }, undefined, undefined, undefined, { preserveAnsi: true }) + }, [], { preserveAnsi: true }) expect(stdout).toContain('true\n') expect(stdout).not.toContain('\x1B[33mtrue\x1B[39m\n') @@ -59,7 +59,7 @@ test('with color, forks pool in TTY parent', async () => { NO_COLOR: undefined, GITHUB_ACTIONS: undefined, }, - }, undefined, undefined, undefined, { preserveAnsi: true }) + }, [], { preserveAnsi: true }) expect(stdout).toContain('\x1B[33mtrue\x1B[39m\n') }) diff --git a/test/config/test/failures.test.ts b/test/config/test/failures.test.ts index d9ec3fb7e..5892572e0 100644 --- a/test/config/test/failures.test.ts +++ b/test/config/test/failures.test.ts @@ -1,6 +1,4 @@ -import type { UserConfig as ViteUserConfig } from 'vite' -import type { TestUserConfig } from 'vitest/node' -import type { VitestRunnerCLIOptions } from '../../test-utils' +import type { RunVitestConfig, VitestRunnerCLIOptions } from '../../test-utils' import { playwright } from '@vitest/browser-playwright' import { preview } from '@vitest/browser-preview' import { webdriverio } from '@vitest/browser-webdriverio' @@ -17,8 +15,12 @@ const providers = [ const names = ['edge', 'chromium', 'webkit', 'chrome', 'firefox', 'safari'] as const const browsers = providers.map(provider => names.map(name => ({ name, provider }))).flat() -function runVitest(config: NonNullable & { shard?: any }, viteOverrides: ViteUserConfig = {}, runnerOptions?: VitestRunnerCLIOptions) { - return testUtils.runVitest({ root: './fixtures/test', include: ['example.test.ts'], ...config }, [], undefined, viteOverrides, runnerOptions) +function runVitest(config: RunVitestConfig, runnerOptions?: VitestRunnerCLIOptions) { + return testUtils.runVitest({ + root: './fixtures/test', + include: ['example.test.ts'], + ...config, + }, [], runnerOptions) } function runVitestCli(...cliArgs: string[]) { @@ -111,15 +113,13 @@ test('inspect and --inspect-brk cannot be used when not playwright + chromium', continue } - const { stderr } = await runVitest({}, { - test: { - [option]: true, - fileParallelism: false, - browser: { - enabled: true, - provider, - instances: [{ browser: name }], - }, + const { stderr } = await runVitest({ + [option]: true, + fileParallelism: false, + browser: { + enabled: true, + provider, + instances: [{ browser: name }], }, }) @@ -162,16 +162,14 @@ test.each( return true }), )('v8 coverage provider throws when $provider.name + $name', async ({ provider, name }) => { - const { stderr } = await runVitest({}, { - test: { - coverage: { - enabled: true, - }, - browser: { - enabled: true, - provider, - instances: [{ browser: name }], - }, + const { stderr } = await runVitest({ + coverage: { + enabled: true, + }, + browser: { + enabled: true, + provider, + instances: [{ browser: name }], }, }) @@ -207,21 +205,19 @@ Use either: }) test('v8 coverage provider throws when using chromium and other non-chromium browser', async () => { - const { stderr } = await runVitest({}, { - test: { - coverage: { - enabled: true, - }, - browser: { - enabled: true, - headless: true, - provider: playwright(), - instances: [ - { browser: 'chromium' }, - { browser: 'firefox' }, - { browser: 'webkit' }, - ], - }, + const { stderr } = await runVitest({ + coverage: { + enabled: true, + }, + browser: { + enabled: true, + headless: true, + provider: playwright(), + instances: [ + { browser: 'chromium' }, + { browser: 'firefox' }, + { browser: 'webkit' }, + ], }, }) @@ -272,7 +268,7 @@ test('v8 coverage provider cannot be used in workspace without chromium', async }, }, ], - }, {}, { fails: true }) + }, { fails: true }) expect(stderr).toMatch( `Error: @vitest/coverage-v8 does not work with { @@ -396,13 +392,11 @@ test('maxConcurrency 0 prints a warning', async () => { }) test('browser.instances is empty', async () => { - const { stderr } = await runVitest({}, { - test: { - browser: { - enabled: true, - provider: playwright(), - instances: [], - }, + const { stderr } = await runVitest({ + browser: { + enabled: true, + provider: playwright(), + instances: [], }, }) expect(stderr).toMatch(`Vitest wasn't able to resolve any project. Please, check that you specified the "browser.instances" option.`) @@ -427,72 +421,64 @@ test('--browser flag without browser configuration in workspaces throws an error }) test('browser.name filters all browser.instances are required', async () => { - const { stderr } = await runVitest({}, { - test: { - browser: { - enabled: true, - name: 'chromium', - provider: playwright(), - instances: [ - { browser: 'firefox' }, - ], - }, + const { stderr } = await runVitest({ + browser: { + enabled: true, + name: 'chromium', + provider: playwright(), + instances: [ + { browser: 'firefox' }, + ], }, }) expect(stderr).toMatch('"browser.instances" was set in the config, but the array is empty. Define at least one browser config. The "browser.name" was set to "chromium" which filtered all configs (firefox). Did you mean to use another name?') }) test('browser.instances throws an error if no custom name is provided', async () => { - const { stderr } = await runVitest({}, { - test: { - browser: { - enabled: true, - provider: playwright(), - instances: [ - { browser: 'firefox' }, - { browser: 'firefox' }, - ], - }, + const { stderr } = await runVitest({ + browser: { + enabled: true, + provider: playwright(), + instances: [ + { browser: 'firefox' }, + { browser: 'firefox' }, + ], }, }) expect(stderr).toMatch('Cannot define a nested project for a firefox browser. The project name "firefox" was already defined. If you have multiple instances for the same browser, make sure to define a custom "name". All projects should have unique names. Make sure your configuration is correct.') }) test('browser.instances throws an error if no custom name is provided, but the config name is inherited', async () => { - const { stderr } = await runVitest({}, { - test: { - name: 'custom', - browser: { - enabled: true, - provider: playwright(), - instances: [ - { browser: 'firefox' }, - { browser: 'firefox' }, - ], - }, + const { stderr } = await runVitest({ + name: 'custom', + browser: { + enabled: true, + provider: playwright(), + instances: [ + { browser: 'firefox' }, + { browser: 'firefox' }, + ], }, }) expect(stderr).toMatch('Cannot define a nested project for a firefox browser. The project name "custom (firefox)" was already defined. If you have multiple instances for the same browser, make sure to define a custom "name". All projects should have unique names. Make sure your configuration is correct.') }) test('throws an error if name conflicts with a workspace name', async () => { - const { stderr } = await runVitest({}, { - test: { - projects: [ - { test: { name: '1 (firefox)' } }, - { - test: { - browser: { - enabled: true, - provider: playwright(), - instances: [ - { browser: 'firefox' }, - ], - }, + const { stderr } = await runVitest({ + projects: [ + { test: { name: '1 (firefox)' } }, + { + test: { + browser: { + enabled: true, + provider: playwright(), + instances: [ + { browser: 'firefox' }, + ], }, }, - ], - }, + }, + ], }) expect(stderr).toMatch('Cannot define a nested project for a firefox browser. The project name "1 (firefox)" was already defined. If you have multiple instances for the same browser, make sure to define a custom "name". All projects should have unique names. Make sure your configuration is correct.') }) diff --git a/test/config/test/pool.test.ts b/test/config/test/pool.test.ts index bed95eafd..749d53d8f 100644 --- a/test/config/test/pool.test.ts +++ b/test/config/test/pool.test.ts @@ -108,11 +108,12 @@ async function getConfig(options: Partial, await runVitest({ root: './fixtures/pool', include: ['print-config.test.ts'], - ...cliOptions, + $cliOptions: cliOptions, onConsoleLog(log) { config = JSON.parse(log) }, - }, undefined, undefined, { test: options }, { }) + ...options, + }) assert(config) return config diff --git a/test/config/test/projects.test.ts b/test/config/test/projects.test.ts index a19a9d3be..c3eb200f6 100644 --- a/test/config/test/projects.test.ts +++ b/test/config/test/projects.test.ts @@ -36,7 +36,7 @@ it('supports glob negation pattern', async () => { it('fails if project names are identical with a nice error message', async () => { const { stderr } = await runVitest({ root: 'fixtures/workspace/invalid-duplicate-configs', - }, [], 'test', {}, { fails: true }) + }, [], { fails: true }) expect(stderr).toContain( `Project name "test" from "vitest.config.two.js" is not unique. The project is already defined by "vitest.config.one.js". @@ -51,7 +51,7 @@ All projects should have unique names. Make sure your configuration is correct.` it('fails if project names are identical inside the inline config', async () => { const { stderr } = await runVitest({ root: 'fixtures/workspace/invalid-duplicate-inline', - }, [], 'test', {}, { fails: true }) + }, [], { fails: true }) expect(stderr).toContain( 'Project name "test" is not unique. All projects should have unique names. Make sure your configuration is correct.', ) @@ -60,7 +60,7 @@ it('fails if project names are identical inside the inline config', async () => it('fails if referenced file doesnt exist', async () => { const { stderr } = await runVitest({ root: 'fixtures/workspace/invalid-non-existing-config', - }, [], 'test', {}, { fails: true }) + }, [], { fails: true }) expect(stderr).toContain( `Projects definition references a non-existing file or a directory: ${resolve('fixtures/workspace/invalid-non-existing-config/vitest.config.js')}`, ) diff --git a/test/config/test/shuffle-options.test.ts b/test/config/test/shuffle-options.test.ts index f9659f048..f50cf1357 100644 --- a/test/config/test/shuffle-options.test.ts +++ b/test/config/test/shuffle-options.test.ts @@ -6,6 +6,8 @@ function run(sequence: InlineConfig['sequence']) { return runVitest({ sequence, include: [], + standalone: true, + watch: true, }) } diff --git a/test/coverage-test/test/isolation.test.ts b/test/coverage-test/test/isolation.test.ts index beb625c58..9b53de697 100644 --- a/test/coverage-test/test/isolation.test.ts +++ b/test/coverage-test/test/isolation.test.ts @@ -27,7 +27,6 @@ for (const isolate of [true, false]) { browser: { isolate, - instances: [], }, }) diff --git a/test/coverage-test/utils.ts b/test/coverage-test/utils.ts index 91e9d7cff..a5ae36319 100644 --- a/test/coverage-test/utils.ts +++ b/test/coverage-test/utils.ts @@ -39,28 +39,25 @@ export async function runVitest(config: TestUserConfig, options = { throwOnError config: 'fixtures/configs/vitest.config.ts', pool: 'threads', ...config, - browser: config.browser, - }, [], 'test', { - ...viteOverrides, - test: { - env: { - COVERAGE_TEST: 'true', - ...config.env, - }, - coverage: { - enabled: true, - reporter: [], - ...config.coverage, - provider, - customProviderModule: provider === 'custom' ? 'fixtures/custom-provider' : undefined, - }, - browser: { - enabled: process.env.COVERAGE_BROWSER === 'true', - headless: true, - instances: [{ browser: 'chromium' }], - provider: playwright(), - }, + env: { + COVERAGE_TEST: 'true', + ...config.env, }, + coverage: { + enabled: true, + reporter: [], + ...config.coverage, + provider, + customProviderModule: provider === 'custom' ? 'fixtures/custom-provider' : undefined, + }, + browser: { + enabled: process.env.COVERAGE_BROWSER === 'true', + headless: true, + instances: [{ browser: 'chromium' }], + provider: playwright(), + ...config.browser, + }, + $viteConfig: viteOverrides, }) if (getCurrentTest()) { diff --git a/test/reporters/tests/dot.test.ts b/test/reporters/tests/dot.test.ts index 9a76f57eb..df11289d5 100644 --- a/test/reporters/tests/dot.test.ts +++ b/test/reporters/tests/dot.test.ts @@ -4,9 +4,9 @@ import { runVitest } from '../../test-utils' describe.each([true, false])('{ isTTY: %s }', (isTTY) => { test('renders successful tests', async () => { const { stdout, stderr } = await runVitest({ - include: ['./fixtures/ok.test.ts'], + root: './fixtures', + include: ['./ok.test.ts'], reporters: [['dot', { isTTY }]], - typecheck: undefined, }) expect(stdout).toContain('\n·\n') @@ -17,9 +17,9 @@ describe.each([true, false])('{ isTTY: %s }', (isTTY) => { test('renders failing tests', async () => { const { stdout, stderr } = await runVitest({ - include: ['./fixtures/some-failing.test.ts'], + root: './fixtures', + include: ['./some-failing.test.ts'], reporters: [['dot', { isTTY }]], - typecheck: undefined, }) expect(stdout).toContain('\n·x\n') @@ -31,9 +31,9 @@ describe.each([true, false])('{ isTTY: %s }', (isTTY) => { test('renders skipped tests', async () => { const { stdout, stderr } = await runVitest({ - include: ['./fixtures/all-skipped.test.ts'], + root: './fixtures', + include: ['./all-skipped.test.ts'], reporters: [['dot', { isTTY }]], - typecheck: undefined, }) expect(stdout).toContain('\n--\n') diff --git a/test/reporters/tests/function-as-name.test.ts b/test/reporters/tests/function-as-name.test.ts index 086442697..97a771dd7 100644 --- a/test/reporters/tests/function-as-name.test.ts +++ b/test/reporters/tests/function-as-name.test.ts @@ -17,7 +17,7 @@ test('should print function name', async () => { test('should print function name in benchmark', async () => { const filename = resolve('./fixtures/function-as-name.bench.ts') - const { stdout } = await runVitest({ root: './fixtures' }, [filename], 'benchmark') + const { stdout } = await runVitest({ root: './fixtures' }, [filename], { mode: 'benchmark' }) expect(stdout).toBeTruthy() expect(stdout).toContain('Bar') diff --git a/test/reporters/tests/html.test.ts b/test/reporters/tests/html.test.ts index 86ae382d4..bc54210a5 100644 --- a/test/reporters/tests/html.test.ts +++ b/test/reporters/tests/html.test.ts @@ -109,16 +109,13 @@ test('basic', () => {}); `, }, { reporters: ['default', 'html'], - }, {}, { - test: { - browser: { - enabled: true, - provider: playwright(), - headless: true, - instances: [ - { browser: 'chromium' as const }, - ], - }, + browser: { + enabled: true, + provider: playwright(), + headless: true, + instances: [ + { browser: 'chromium' as const }, + ], }, }) expect(result.errorTree()).toMatchInlineSnapshot(` diff --git a/test/reporters/tests/logger.test.ts b/test/reporters/tests/logger.test.ts index d4cca4e86..6c07a1761 100644 --- a/test/reporters/tests/logger.test.ts +++ b/test/reporters/tests/logger.test.ts @@ -7,7 +7,7 @@ test('cursor is hidden during test run in TTY', async () => { root: 'fixtures/default', reporters: 'none', watch: false, - }, undefined, undefined, undefined, { tty: true, preserveAnsi: true }) + }, [], { tty: true, preserveAnsi: true }) expect(stdout).toContain('\x1B[?25l') expect(stdout).toContain('\x1B[?25h') @@ -19,7 +19,7 @@ test('cursor is not hidden during test run in non-TTY', async () => { root: 'fixtures/default', reporters: 'none', watch: false, - }, undefined, undefined, undefined, { preserveAnsi: true }) + }, [], { preserveAnsi: true }) expect(stdout).not.toContain('\x1B[?25l') expect(stdout).not.toContain('\x1B[?25h') diff --git a/test/snapshots/test/fixtures/compare-keys/vitest.config.ts b/test/snapshots/test/fixtures/compare-keys/vitest.config.ts index 4c2e06d40..821285a37 100644 --- a/test/snapshots/test/fixtures/compare-keys/vitest.config.ts +++ b/test/snapshots/test/fixtures/compare-keys/vitest.config.ts @@ -2,8 +2,8 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { - snapshotFormat: { - // compareKeys: null, - } + // snapshotFormat: { + // compareKeys: null, + // } } }) diff --git a/test/test-utils/index.ts b/test/test-utils/index.ts index b24a22d02..b0ff7ce55 100644 --- a/test/test-utils/index.ts +++ b/test/test-utils/index.ts @@ -9,7 +9,6 @@ import type { TestSpecification, TestUserConfig, Vitest, - VitestRunMode, } from 'vitest/node' import { webcrypto as crypto } from 'node:crypto' import fs from 'node:fs' @@ -34,13 +33,27 @@ export interface VitestRunnerCLIOptions { fails?: boolean preserveAnsi?: boolean tty?: boolean + mode?: 'test' | 'benchmark' } +export interface RunVitestConfig extends TestUserConfig { + $viteConfig?: Omit + $cliOptions?: TestUserConfig +} + +/** + * The config is assumed to be the config on the fille system, not CLI options + * (Note that CLI only options like "standalone" are passed as CLI options, not config options) + * - To pass options as CLI, provide `$cliOptions` in the config object. + * - To pass other Vite config properties, provide `$viteConfig` in the config object. + * + * **WARNING** + * If the fixture in `root` has a config file, its options **WILL TAKE PRIORITY** over the ones provided here, + * except for the ones provided in `$cliOptions`. + */ export async function runVitest( - cliOptions: TestUserConfig, + config: RunVitestConfig, cliFilters: string[] = [], - mode: VitestRunMode = 'test', - viteOverrides: ViteUserConfig = {}, runnerOptions: VitestRunnerCLIOptions = {}, ) { // Reset possible previous runs @@ -83,28 +96,76 @@ export async function runVitest( let ctx: Vitest | undefined let thrown = false + + const { + reporters, + root, + watch, + maxWorkers, + // #region cli-only options + config: configFile, + standalone, + dom, + related, + mode, + changed, + shard, + project, + cliExclude, + clearScreen, + compare, + outputJson, + mergeReports, + clearCache, + // #endregion + $cliOptions: cliOptions, + $viteConfig: viteConfig = {}, + ...rest + } = config + + if ((viteConfig as any).test) { + throw new Error(`Don't pass down "viteConfig" with "test" property. Use the rest of the first argument.`) + } + + ;(viteConfig as any).test = rest + try { - const { reporters, ...rest } = cliOptions + ctx = await startVitest(runnerOptions.mode || 'test', cliFilters, { + root, + config: configFile, + standalone, + dom, + related, + mode, + changed, + shard, + project, + cliExclude, + clearScreen, + compare, + outputJson, + mergeReports, + clearCache, - ctx = await startVitest(mode, cliFilters, { // Test cases are already run with multiple forks/threads - maxWorkers: 1, + maxWorkers: maxWorkers ?? 1, - watch: false, + watch: watch ?? false, // "none" can be used to disable passing "reporter" option so that default value is used (it's not same as reporters: ["default"]) ...(reporters === 'none' ? {} : reporters ? { reporters } : { reporters: ['verbose'] }), - ...rest, + ...cliOptions, env: { NO_COLOR: 'true', ...rest.env, + ...cliOptions?.env, }, // override cache config with the one that was used to run `vitest` formt the CLI experimental: { - fsModuleCache: currentConfig.experimental.fsModuleCache, - ...rest.experimental, + fsModuleCache: rest.experimental?.fsModuleCache ?? currentConfig.experimental.fsModuleCache, + ...cliOptions?.experimental, }, }, { - ...viteOverrides, + ...viteConfig, server: { // we never need a websocket connection for the root config because it doesn't connect to the browser // browser mode uses a separate config that doesn't inherit CLI overrides @@ -115,8 +176,9 @@ export async function runVitest( // https://github.com/vitejs/vite/blob/b723a753ced0667470e72b4853ecda27b17f546a/playground/vitestSetup.ts#L211 usePolling: true, interval: 100, + ...viteConfig.server?.watch, }, - ...viteOverrides?.server, + ...viteConfig?.server, }, }, { stdin, @@ -387,16 +449,15 @@ export function useFS(root: string, structure: T, ens export async function runInlineTests( structure: TestFsStructure, - config?: TestUserConfig, + config?: RunVitestConfig, options?: VitestRunnerCLIOptions, - viteOverrides: ViteUserConfig = {}, ) { const root = resolve(process.cwd(), `vitest-test-${crypto.randomUUID()}`) const fs = useFS(root, structure) const vitest = await runVitest({ root, ...config, - }, [], 'test', viteOverrides, options) + }, [], options) return { fs, root, diff --git a/test/watch/test/file-watching.test.ts b/test/watch/test/file-watching.test.ts index 39416e8bd..97817c451 100644 --- a/test/watch/test/file-watching.test.ts +++ b/test/watch/test/file-watching.test.ts @@ -191,13 +191,12 @@ describe('browser', () => { const { vitest } = await testUtils.runVitest({ root: 'fixtures', watch: true, - }, undefined, undefined, { - test: { browser: { + browser: { instances: [{ browser: 'chrome' }], provider: webdriverio(), enabled: true, headless: true, - } }, + }, }) writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')