{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/benchmark.ts#L581-L607","documentation":"Benchmarks are noisy, so Vitest runs them in a dedicated project matched by `benchmark.include` (default `**/*.{bench,benchmark}.?(c|m)[jt]s?(x)`) rather than in regular test runs. `validateBenchmarkProject` (`benchmark.ts:599`) throws when `config.benchmark.enabled` is false — i.e. the `bench` fixture is used in a non-benchmark run. `bench` is a test-context fixture (`async ({ bench }) => ...`), NOT a top-level export of `vitest`.","triggerScenarios":"Importing `{ bench }` from `'vitest'` in a regular `.test.ts` file; destructuring `bench` from the context inside a file not matched by `benchmark.include`; running a `.bench.ts` file via the normal test command without a benchmark project configured.","commonSituations":"Writing benchmarks in a normal test file; top-level `import { bench } from 'vitest'`; missing a benchmark project in the config; running with `--project` pointing at a non-benchmark project.","solutions":["Rename the file to `*.bench.ts` (or whatever matches `benchmark.include`).","Access `bench` via the test context: `test('...', async ({ bench }) => { ... })` — never as a top-level import.","Ensure a benchmark project exists (define one with `test.benchmark` in the config) and run with `--project <name>` if needed."],"exampleFix":"// before (utils.test.ts) — regular test file\nimport { test } from 'vitest'\ntest('sort speed', () => {\n  bench('sort', () => sortArr()) // bench not available here\n})\n\n// after (sort.bench.ts) — file matches benchmark.include\ntest('sort speed', async ({ bench }) => {\n  bench('sort', () => sortArr())\n})","handlingStrategy":"validation","validationCode":"// Gate benchmark code on whether benchmarks are enabled for this run.\n// Easiest signal: the file extension matches benchmark.include.\nconst isBenchFile = /\\.(bench|benchmark)\\.[mc]?[tj]sx?$/.test(__filename ?? '')\nif (!isBenchFile) {\n  throw new Error('Move this benchmark into a *.bench.ts file')\n}\n// And always destructure bench from the context:\ntest('x', async ({ bench }) => { /* ... */ })","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Name benchmark files *.bench.ts / *.benchmark.ts to match benchmark.include.","Never import { bench } from 'vitest' at module top level — it's a context fixture.","Always destructure bench from the test callback: async ({ bench }) => ...","Configure a dedicated benchmark project in vitest.config and run it explicitly."],"tags":["benchmark","configuration","project-setup","context-fixture"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}