vitest-dev/vitest · error · TypeError

File is required to collect tasks.

Error message

File is required to collect tasks.

What it means

TypeError thrown by SuiteCollector.collect when called with a falsy `file` argument. Collection binds tasks to a file scope for stack traces, location tracking, and reporting; without a file the collector cannot proceed. This is an internal-API guard: end users rarely call collect() directly.

Solutions

  1. Ensure the File object (with filepath) is constructed and passed before calling collect.
  2. Validate the file argument at the boundary of your custom code.
  3. Use the public test/describe APIs which handle file wiring automatically.

Example fix

// before
collector.collect(undefined) // throws

// after
const file: File = { filepath: '/abs/path.test.ts', ... }
await collector.collect(file)
Defensive patterns

Strategy: validation

Validate before calling

async function safeCollect(collector, file?: File) {
  if (!file) throw new TypeError('collect requires a File')
  return collector.collect(file)
}

Type guard

const hasFilepath = (f?: File): f is File => !!f?.filepath

Prevention

When it happens

Trigger: Calling suiteCollector.collect(undefined) from custom collector code; a wrapper that forwards a missing file; a plugin that intercepts collection and passes a null file.

Common situations: Building a custom collector or plugin; programmatic use of the runner internals where the file path was not resolved; mocking file during tests of the collector itself.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/2f7d3d9622cb8562. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/runtime/runner/suite.ts:542

      if (stack) {
        suite.location = {
          line: stack.line,
          column: stack.column,
        }
      }
    }

    setHooks(suite, createSuiteHooks())
  }

  function clear() {
    tasks.length = 0
    initSuite(false)
  }

  async function collect(file: File) {
    if (!file) {
      throw new TypeError('File is required to collect tasks.')
    }

    if (factory) {
      await runWithSuite(collector, () => factory(test))
    }

    const allChildren: Task[] = []

    let containsOnly = false
    let containsTest = false
    for (const i of tasks) {
      const child = i.type === 'collector' ? await i.collect(file) : i
      allChildren.push(child)
      if (child.mode === 'only' || (child.type === 'suite' && child.containsOnly)) {
        containsOnly = true
      }
      if (child.type === 'test' || (child.type === 'suite' && child.containsTest)) {
        containsTest = true

View on GitHub (pinned to 1fa9837ec2)