{"record":{"id":"e17aed2ac24cecdf","repo":"vitest-dev/vitest","slug":"this-command-can-only-be-called-inside-a-test-file","errorCode":null,"errorMessage":"This command can only be called inside a test file.","messagePattern":"This command can only be called inside a test file\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser-playwright/src/commands/trace.ts","lineNumber":144,"sourceCode":"      return\n    }\n    await context.context.tracing.groupEnd()\n    return\n  }\n  throw new TypeError(`The ${context.provider.name} provider does not support tracing.`)\n}\n\nfunction parseLocation(context: BrowserCommandContext, stack?: string): ParsedStack | undefined {\n  if (!stack) {\n    return\n  }\n  const parsedStacks = context.project.browser!.parseStacktrace(stack)\n  return parsedStacks[0]\n}\n\nfunction resolveTracesPath({ testPath, project }: BrowserCommandContext, name: string) {\n  if (!testPath) {\n    throw new Error(`This command can only be called inside a test file.`)\n  }\n  const options = project.config.browser!.trace\n  const sanitizedName = `${project.name.replace(/[^a-z0-9]/gi, '-')}-${name}.trace.zip`\n  if (options.tracesDir) {\n    return resolve(options.tracesDir, sanitizedName)\n  }\n  const dir = dirname(testPath)\n  const base = basename(testPath)\n  return resolve(\n    dir,\n    '__traces__',\n    base,\n    `${project.name.replace(/[^a-z0-9]/gi, '-')}-${name}.trace.zip`,\n  )\n}\n\nexport const deleteTracing: BrowserCommand<[{ traces: string[] }]> = async (\n  context,","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/browser-playwright/src/commands/trace.ts#L126-L162","documentation":"resolveTracesPath is an internal helper that computes the on-disk path for a trace zip: either under `browser.trace.tracesDir` or beside the test file in `__traces__/<testFile>/`. It needs testPath to build the default directory. When called outside a test (testPath undefined) it throws. This is reached indirectly via startChunkTrace/stopChunkTrace.","triggerScenarios":"A trace chunk command (start/stop) is dispatched with a BrowserCommandContext that has no testPath — e.g. during global hooks, between tests, or from a non-browser pool.","commonSituations":"Tracing enabled but the test is run in a context without a bound file (global teardown, manual invocation of internal trace APIs).","solutions":["Ensure trace chunk commands run only inside an executing test (handled by the runner — if seen, you are calling internals directly or hit a runner bug).","Run the file under `pool: 'browser'` and matching `include` so testPath is set.","Set `browser.trace.tracesDir` for deterministic output, but note the testPath guard still applies."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// internal helper guard — only relevant if you call resolveTracesPath yourself\nfunction safeResolveTracesPath(ctx: { testPath?: string }) {\n  if (!ctx.testPath) throw new Error('resolveTracesPath requires a testPath')\n  // ...\n}","typeGuard":"function hasTestPath<T extends { testPath?: string }>(c: T): c is T & { testPath: string } {\n  return !!c.testPath\n}","tryCatchPattern":null,"preventionTips":["Run trace-related files under pool: 'browser' with valid includes.","Do not invoke internal trace path helpers directly.","If seen during normal runs, report — the runner should always supply testPath here."],"tags":["browser","tracing","lifecycle","internal","filesystem"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}