{"record":{"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":542,"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    let containsOnly = false\n    let containsTest = false\n    for (const i of tasks) {\n      const child = i.type === 'collector' ? await i.collect(file) : i\n      allChildren.push(child)\n      if (child.mode === 'only' || (child.type === 'suite' && child.containsOnly)) {\n        containsOnly = true\n      }\n      if (child.type === 'test' || (child.type === 'suite' && child.containsTest)) {\n        containsTest = true","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/runner/suite.ts#L524-L560","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the File object (with filepath) is constructed and passed before calling collect.","Validate the file argument at the boundary of your custom code.","Use the public test/describe APIs which handle file wiring automatically."],"exampleFix":"// before\ncollector.collect(undefined) // throws\n\n// after\nconst file: File = { filepath: '/abs/path.test.ts', ... }\nawait collector.collect(file)","handlingStrategy":"validation","validationCode":"async function safeCollect(collector, file?: File) {\n  if (!file) throw new TypeError('collect requires a File')\n  return collector.collect(file)\n}","typeGuard":"const hasFilepath = (f?: File): f is File => !!f?.filepath","tryCatchPattern":null,"preventionTips":["Always construct and pass the File object when using collector APIs.","Prefer public test/describe APIs which wire the file automatically.","In plugin code, resolve the file path before invoking collect."],"tags":["collector","internal","validation"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}