{"id":"6e17a2d9052cdf11","repo":"vitest-dev/vitest","slug":"stopchunktrace-cannot-be-called-outside-of-the-tes","errorCode":null,"errorMessage":"stopChunkTrace cannot be called outside of the test file.","messagePattern":"stopChunkTrace cannot be called outside of the test file\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser-playwright/src/commands/trace.ts","lineNumber":36,"sourceCode":"    await context.tracing.start({\n      screenshots: options.screenshots ?? true,\n      snapshots: options.snapshots ?? true,\n      sources: options.sources ?? true,\n    }).catch(() => {\n      provider.tracingContexts.delete(sessionId)\n    })\n    return\n  }\n  throw new TypeError(`The ${provider.name} provider does not support tracing.`)\n}\n\nexport const startChunkTrace: BrowserCommand<[{ name: string; title: string }]> = async (\n  command,\n  { name, title },\n) => {\n  const { provider, sessionId, testPath, context } = command\n  if (!testPath) {\n    throw new Error(`stopChunkTrace cannot be called outside of the test file.`)\n  }\n  if (isPlaywrightProvider(provider)) {\n    if (!provider.tracingContexts.has(sessionId)) {\n      await startTracing(command)\n    }\n    const path = resolveTracesPath(command, name)\n    provider.pendingTraces.set(path, sessionId)\n    await context.tracing.startChunk({ name, title })\n    return\n  }\n  throw new TypeError(`The ${provider.name} provider does not support tracing.`)\n}\n\nexport const stopChunkTrace: BrowserCommand<[{ name: string }]> = async (\n  context,\n  { name },\n) => {\n  if (isPlaywrightProvider(context.provider)) {","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser-playwright/src/commands/trace.ts#L18-L54","documentation":"Thrown by the startChunkTrace browser command when context.testPath is falsy. Per-chunk traces are written relative to the test file (the trace.zip is placed alongside the test), so Vitest requires a concrete test file path. Note: the message literally says 'stopChunkTrace' but the throw lives in startChunkTrace (a copy-paste artifact in the source).","triggerScenarios":"Invoking startChunkTrace from a setup file, global hook, or a context where Vitest did not bind a testPath. The trace chunk cannot be named/placed without the owning test file.","commonSituations":"Calling tracing APIs in beforeAll/globalSetup, or from a custom command executed outside the test task graph. Also seen when chunk tracing is triggered during teardown after the test context is cleared.","solutions":["Call startChunkTrace from within an it/test block so a testPath is bound.","Avoid invoking tracing primitives from setup/teardown files that have no owning test.","If you need trace-like output in setup, use Playwright's tracing.start directly on the context."],"exampleFix":"// before (globalSetup or beforeAll)\nawait context.commands.startChunkTrace({ name: 'boot', title: 'boot' })\n\n// after (inside a test)\ntest('boot trace', async () => {\n  await context.commands.startChunkTrace({ name: 'boot', title: 'boot' })\n})","handlingStrategy":"validation","validationCode":"if (!context.testPath) {\n  throw new Error('startChunkTrace must be called from a test with a bound testPath')\n}\nawait startChunkTrace(command, { name, title })","typeGuard":"function inTestFile(c: BrowserCommandContext): c is BrowserCommandContext & { testPath: string } {\n  return typeof c.testPath === 'string' && c.testPath.length > 0\n}","tryCatchPattern":null,"preventionTips":["Call chunk-trace APIs only from within it/test bodies.","Do not issue tracing primitives from setup/global hooks.","Use Playwright's tracing.start directly for setup-time trace needs."],"tags":["browser","tracing","test-context","playwright","vitest"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}