diff --git a/packages/vitest/src/runtime/collect.ts b/packages/vitest/src/runtime/collect.ts index 1966071ef..de03c755b 100644 --- a/packages/vitest/src/runtime/collect.ts +++ b/packages/vitest/src/runtime/collect.ts @@ -110,7 +110,7 @@ function interpretTaskModes(suite: Suite, namePattern?: string | RegExp, onlyMod // if all subtasks are skipped, mark as skip if (suite.mode === 'run') { - if (suite.tasks.every(i => i.mode !== 'run')) + if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run')) suite.mode = 'skip' } } diff --git a/packages/vitest/src/runtime/run.ts b/packages/vitest/src/runtime/run.ts index 195c10e54..6fbf251a3 100644 --- a/packages/vitest/src/runtime/run.ts +++ b/packages/vitest/src/runtime/run.ts @@ -1,5 +1,5 @@ import { performance } from 'perf_hooks' -import type { HookListener, ResolvedConfig, Suite, SuiteHooks, Task, TaskResult, Test } from '../types' +import type { File, HookListener, ResolvedConfig, Suite, SuiteHooks, Task, TaskResult, Test } from '../types' import { vi } from '../integrations/vi' import { getSnapshotClient } from '../integrations/snapshot/chai' import { getFullName, hasFailed, hasTests, partitionSuiteChildren } from '../utils' @@ -196,9 +196,17 @@ async function runSuiteChild(c: Task) { : runSuite(c) } -export async function runSuites(suites: Suite[]) { - for (const suite of suites) - await runSuite(suite) +export async function runFiles(files: File[], config: ResolvedConfig) { + for (const file of files) { + if (!file.tasks.length && !config.passWithNoTests) { + file.result = { + state: 'fail', + error: new Error(`No test suite found in file ${file.filepath}`), + } + } + + await runSuite(file) + } } export async function startTests(paths: string[], config: ResolvedConfig) { @@ -206,7 +214,7 @@ export async function startTests(paths: string[], config: ResolvedConfig) { rpc().onCollected(files) - await runSuites(files) + await runFiles(files, config) takeCoverage() diff --git a/test/fails/fixtures/empty.test.ts b/test/fails/fixtures/empty.test.ts new file mode 100644 index 000000000..e69de29bb diff --git a/test/fails/test/__snapshots__/runner.test.ts.snap b/test/fails/test/__snapshots__/runner.test.ts.snap index 60a1c35db..fc01ca968 100644 --- a/test/fails/test/__snapshots__/runner.test.ts.snap +++ b/test/fails/test/__snapshots__/runner.test.ts.snap @@ -1,5 +1,7 @@ // Vitest Snapshot v1 +exports[`should fails > empty.test.ts > empty.test.ts 1`] = `"Error: No test suite found in file /Users/antfu/i/vitest/test/fails/fixtures/empty.test.ts"`; + exports[`should fails > expect.test.ts > expect.test.ts 1`] = `"AssertionError: expected 2 to deeply equal 3"`; exports[`should fails > hook-timeout.test.ts > hook-timeout.test.ts 1`] = `"Error: Hook timed out in 10ms."`;