{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser-playwright/src/commands/trace.ts#L126-L162","documentation":"Thrown by the resolveTracesPath helper (used by startChunkTrace/stopChunkTrace) when testPath is missing. The helper computes the trace.zip output directory from dirname(testPath) and the test filename; without a test file it cannot place the trace.","triggerScenarios":"Any tracing command that resolves an output path is invoked from a context without a bound testPath (setup, global hooks, or a synthesized command context).","commonSituations":"Calling tracing helpers from beforeAll/globalSetup, or a plugin that issues trace commands outside the test task graph.","solutions":["Invoke tracing commands only from within a running test so testPath is set.","Move the tracing call into the it/test body.","For non-test scenarios, write traces manually via Playwright's tracing API with an explicit dir."],"exampleFix":"// before (no test context)\nawait context.commands.stopChunkTrace({ name: 'boot' })\n\n// after (inside a test)\ntest('traced', async () => {\n  await context.commands.stopChunkTrace({ name: 'traced' })\n})","handlingStrategy":"validation","validationCode":"if (!context.testPath) {\n  throw new Error('trace path resolution requires a test file context')\n}\nresolveTracesPath(context, name)","typeGuard":"function inTestFile(c: BrowserCommandContext): c is BrowserCommandContext & { testPath: string } {\n  return typeof c.testPath === 'string' && c.testPath.length > 0\n}","tryCatchPattern":null,"preventionTips":["Only call path-resolving trace helpers from within a test.","Avoid tracing calls in setup/global hooks.","For non-test trace output, write via Playwright tracing with an explicit dir."],"tags":["browser","tracing","test-context","playwright","vitest"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}