vitest-dev/vitest · error · Error
This command can only be called inside a test file.
Error message
This command can only be called inside a test file.
What it means
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.
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.
Defensive patterns
Strategy: validation
Validate before calling
// internal helper guard — only relevant if you call resolveTracesPath yourself
function safeResolveTracesPath(ctx: { testPath?: string }) {
if (!ctx.testPath) throw new Error('resolveTracesPath requires a testPath')
// ...
} Type guard
function hasTestPath<T extends { testPath?: string }>(c: T): c is T & { testPath: string } {
return !!c.testPath
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Tracing enabled but the test is run in a context without a bound file (global teardown, manual invocation of internal trace APIs).
Related errors
- provider is not supported
- stopChunkTrace cannot be called outside of the test file.
- Browser is not initialized
- Access denied to " ". See Vite config documentation for…
- Browser provider is not defined for the project
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/e17aed2ac24cecdf.
Report an issue: GitHub.
Appendix: source
Thrown at packages/browser-playwright/src/commands/trace.ts:144
return
}
await context.context.tracing.groupEnd()
return
}
throw new TypeError(`The ${context.provider.name} provider does not support tracing.`)
}
function parseLocation(context: BrowserCommandContext, stack?: string): ParsedStack | undefined {
if (!stack) {
return
}
const parsedStacks = context.project.browser!.parseStacktrace(stack)
return parsedStacks[0]
}
function resolveTracesPath({ testPath, project }: BrowserCommandContext, name: string) {
if (!testPath) {
throw new Error(`This command can only be called inside a test file.`)
}
const options = project.config.browser!.trace
const sanitizedName = `${project.name.replace(/[^a-z0-9]/gi, '-')}-${name}.trace.zip`
if (options.tracesDir) {
return resolve(options.tracesDir, sanitizedName)
}
const dir = dirname(testPath)
const base = basename(testPath)
return resolve(
dir,
'__traces__',
base,
`${project.name.replace(/[^a-z0-9]/gi, '-')}-${name}.trace.zip`,
)
}
export const deleteTracing: BrowserCommand<[{ traces: string[] }]> = async (
context,View on GitHub (pinned to 1fa9837ec2)