{"id":"2f7d3d9622cb8562","repo":"vitest-dev/vitest","slug":"file-is-required-to-collect-tasks","errorCode":null,"errorMessage":"File is required to collect tasks.","messagePattern":"File is required to collect tasks\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/runner/suite.ts","lineNumber":546,"sourceCode":"      if (stack) {\n        suite.location = {\n          line: stack.line,\n          column: stack.column,\n        }\n      }\n    }\n\n    setHooks(suite, createSuiteHooks())\n  }\n\n  function clear() {\n    tasks.length = 0\n    initSuite(false)\n  }\n\n  async function collect(file: File) {\n    if (!file) {\n      throw new TypeError('File is required to collect tasks.')\n    }\n\n    if (factory) {\n      await runWithSuite(collector, () => factory(test))\n    }\n\n    const allChildren: Task[] = []\n\n    for (const i of tasks) {\n      allChildren.push(i.type === 'collector' ? await i.collect(file) : i)\n    }\n\n    suite.tasks = allChildren\n\n    return suite\n  }\n\n  collectTask(collector)","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/runner/suite.ts#L528-L564","documentation":"The `SuiteCollector.collect` method (suite.ts:544-547) requires a `File` to attach collected tasks to. If `file` is null/undefined when `collect` is invoked, it throws a `TypeError`. This is an internal collection-path guard; normally the file is always provided by the orchestrator during `collectTests`.","triggerScenarios":"Programmatically calling `collector.collect(undefined)` or `collector.collect(null)`; a custom collection pipeline that doesn't pass the file; a malformed `FileSpecification` that yields no file object.","commonSituations":"Custom runners/builders that drive collection manually; bugs in tooling that constructs `SuiteCollector` without a file context; edge cases in programmatic API usage.","solutions":["Always pass a valid `File` object when calling `collector.collect(file)`.","Use `collectTests(specs, runner)` which constructs and passes files internally.","Validate that your `FileSpecification` resolves to a non-null file before collection."],"exampleFix":"// before\nconst collector = suite('x', () => {})\nawait collector.collect(undefined) // throws\n// after\nconst file = { filepath: '/abs/test.ts', /* ...required File fields */ } as File\nawait collector.collect(file)","handlingStrategy":"validation","validationCode":"import type { File } from 'vitest'\nfunction collectSafely(c: { collect(f: File): Promise<any> }, file: File | null | undefined) {\n  if (!file) throw new TypeError('A valid File is required')\n  return c.collect(file)\n}","typeGuard":"function isFile(v: unknown): v is File {\n  return !!v && typeof (v as any).filepath === 'string'\n}","tryCatchPattern":null,"preventionTips":["Always pass a fully-constructed File to collector.collect.","Prefer collectTests(specs, runner) over manual collection.","Validate FileSpecification resolution before driving collection."],"tags":["collection","internal","file","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}