{"record":{"id":"e253a42df00e7cbf","repo":"vitest-dev/vitest","slug":"cannot-use-the-bench-test-context-fixture-within","errorCode":null,"errorMessage":"Cannot use the `bench` test-context fixture within a regular test run. Benchmarks are inherently flaky, so Vitest runs them in a dedicated project based on the `benchmark.include` pattern (default `**/*.{bench,benchmark}.?(c|m)[jt]s?(x)`). Move this code to a file matched by `benchmark.include`, and make sure `bench` is destructured from the test context (`test('...', async ({ bench }) => { ... })`) — it is not a top-level export of `vitest`. See https://vitest.dev/guide/benchmarking#stability","messagePattern":"Cannot use the `bench` test-context fixture within a regular test run\\. Benchmarks are inherently flaky, so Vitest runs them in a dedicated project based on the `benchmark\\.include` pattern \\(default `\\*\\*/\\*\\.\\{bench,benchmark\\}\\.\\?\\(c\\|m\\)\\[jt\\]s\\?\\(x\\)`\\)\\. Move this code to a file matched by `benchmark\\.include`, and make sure `bench` is destructured from the test context \\(`test\\('\\.\\.\\.', async \\(\\{ bench \\}\\) => \\{ \\.\\.\\. \\}\\)`\\) — it is not a top-level export of `vitest`\\. See https://vitest\\.dev/guide/benchmarking#stability","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":599,"sourceCode":"  // Strip vitest-specific fields only when present so we don't allocate a new\n  // object — preserving referential identity matters: users inspect\n  // `registration.fnOpts` and the provider sees the same object the caller\n  // passed in.\n  if (a.writeResult === undefined && a.perProject === undefined) {\n    return { fn: b, fnOpts: a as BenchOptions, writeResult: undefined, perProject: false }\n  }\n  const { writeResult, perProject, ...fnOpts } = a\n  return {\n    fn: b,\n    fnOpts: Object.keys(fnOpts).length > 0 ? fnOpts as BenchOptions : undefined,\n    writeResult,\n    perProject: perProject ?? false,\n  }\n}\n\nfunction validateBenchmarkProject(config: SerializedConfig) {\n  if (!config.benchmark.enabled) {\n    throw new Error(\n      `Cannot use the \\`bench\\` test-context fixture within a regular test run. `\n      + `Benchmarks are inherently flaky, so Vitest runs them in a dedicated project based on the \\`benchmark.include\\` pattern (default \\`**/*.{bench,benchmark}.?(c|m)[jt]s?(x)\\`). `\n      + `Move this code to a file matched by \\`benchmark.include\\`, and make sure \\`bench\\` is destructured from the test context (\\`test('...', async ({ bench }) => { ... })\\`) — it is not a top-level export of \\`vitest\\`. `\n      + `See https://vitest.dev/guide/benchmarking#stability`,\n    )\n  }\n}\n","sourceCodeStart":581,"sourceCodeEnd":607,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/benchmark.ts#L581-L607","documentation":"The `bench` fixture is only available on the test context inside projects whose `benchmark.enabled` config is true (i.e. files matched by `benchmark.include`). Vitest isolates benchmarks into a dedicated project because they are inherently flaky. This error fires when `bench` is destructured and used inside a regular (non-benchmark) test run — `config.benchmark.enabled` is false.","triggerScenarios":"Destructuring `{ bench }` from the test context in a file NOT matched by `benchmark.include` (default `**/*.{bench,benchmark}.?(c|m)[jt]s?(x)`); running `vitest` (not `vitest bench`) on a file that uses the `bench` fixture; importing `bench` as a top-level export from `vitest` (it is not one). The guard is `validateBenchmarkProject` at benchmark.ts:597-605.","commonSituations":"Putting benchmark code in a `*.test.ts` file; running the default `vitest` command instead of `vitest bench`; a project config that overrides `include` to also match bench files but does not set `benchmark.enabled`; trying `import { bench } from 'vitest'`.","solutions":["Move the benchmark code into a file matching `benchmark.include` (e.g. `*.bench.ts`).","Run with `vitest bench` (or `vitest --project <bench-project>`) so `benchmark.enabled` is true.","Ensure the bench project is configured: `benchmark: { include: ['**/*.bench.ts'] }` and the file is matched.","Access `bench` only via the test context: `test('...', async ({ bench }) => { ... })` — never as a top-level import."],"exampleFix":"// before - src/sort.test.ts (run via `vitest`)\ntest('sort perf', async ({ bench }) => {\n  bench('sort', sortFn)\n})\n\n// after - src/sort.bench.ts (run via `vitest bench`)\ntest('sort perf', async ({ bench }) => {\n  bench('sort', sortFn)\n})","handlingStrategy":"validation","validationCode":"if (!config.benchmark.enabled) throw new Error('run via `vitest bench`; this file is not in benchmark.include')","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Name benchmark files `*.bench.ts` to match the default `benchmark.include`.","Run benchmarks with `vitest bench`, not `vitest`.","Never `import { bench } from 'vitest'` — it is a context fixture only.","Configure a dedicated bench project in the workspace config."],"tags":["benchmark","configuration","project-setup","fixture"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}