vitest-dev/vitest · error · Error

stopChunkTrace cannot be called outside of the test file.

Error message

stopChunkTrace cannot be called outside of the test file.

What it means

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).

Source

Thrown at packages/browser-playwright/src/commands/trace.ts:36

    await context.tracing.start({
      screenshots: options.screenshots ?? true,
      snapshots: options.snapshots ?? true,
      sources: options.sources ?? true,
    }).catch(() => {
      provider.tracingContexts.delete(sessionId)
    })
    return
  }
  throw new TypeError(`The ${provider.name} provider does not support tracing.`)
}

export const startChunkTrace: BrowserCommand<[{ name: string; title: string }]> = async (
  command,
  { name, title },
) => {
  const { provider, sessionId, testPath, context } = command
  if (!testPath) {
    throw new Error(`stopChunkTrace cannot be called outside of the test file.`)
  }
  if (isPlaywrightProvider(provider)) {
    if (!provider.tracingContexts.has(sessionId)) {
      await startTracing(command)
    }
    const path = resolveTracesPath(command, name)
    provider.pendingTraces.set(path, sessionId)
    await context.tracing.startChunk({ name, title })
    return
  }
  throw new TypeError(`The ${provider.name} provider does not support tracing.`)
}

export const stopChunkTrace: BrowserCommand<[{ name: string }]> = async (
  context,
  { name },
) => {
  if (isPlaywrightProvider(context.provider)) {

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Call startChunkTrace from within an it/test block so a testPath is bound.
  2. Avoid invoking tracing primitives from setup/teardown files that have no owning test.
  3. If you need trace-like output in setup, use Playwright's tracing.start directly on the context.

Example fix

// before (globalSetup or beforeAll)
await context.commands.startChunkTrace({ name: 'boot', title: 'boot' })

// after (inside a test)
test('boot trace', async () => {
  await context.commands.startChunkTrace({ name: 'boot', title: 'boot' })
})
Defensive patterns

Strategy: validation

Validate before calling

if (!context.testPath) {
  throw new Error('startChunkTrace must be called from a test with a bound testPath')
}
await startChunkTrace(command, { name, title })

Type guard

function inTestFile(c: BrowserCommandContext): c is BrowserCommandContext & { testPath: string } {
  return typeof c.testPath === 'string' && c.testPath.length > 0
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/6e17a2d9052cdf11.json. Report an issue: GitHub.