vitest-dev/vitest · error · Error
Calling the suite function inside test function is not…
Error message
Calling the suite function inside test function is not allowed. It can be only called at the top level or inside another suite function.
What it means
Thrown by the suite()/describe() factory when getCurrentTest() is truthy, i.e., describe is called inside a running test body. Suites must be defined statically during collection (top-level or nested in another suite factory); defining a suite at runtime has no collection phase to absorb it. The guard mirrors the equivalent check on test().
Solutions
- Move the describe/suite call to the top level or inside another describe factory.
- For dynamic suites, generate them at module load or inside describe, not inside test.
- If behavior must vary per test, branch inside the test body rather than defining new suites.
Example fix
// before
test('x', () => {
describe('group', () => { /* ... */ }) // throws
})
// after
describe('group', () => {
test('x', () => {})
}) Defensive patterns
Strategy: validation
Validate before calling
import { getCurrentTest } from 'vitest'
function defineSuite(name, factory) {
if (getCurrentTest()) throw new Error('describe() cannot be called inside a test')
return describe(name, factory)
} Type guard
const atSuiteScope = () => !getCurrentTest()
Prevention
- Define describe/suite at the top level or nested in another describe only.
- Do not call describe helpers from test bodies.
- Branch inside tests rather than generating suites at runtime.
When it happens
Trigger: Writing `test('x', () => { describe('inner', () => {...}) })`; calling a helper that calls describe() from within a test; generating suites conditionally at runtime.
Common situations: Converting static config into runtime-generated suites; helper functions that wrap describe and are invoked from tests; framework migration where runtime nesting was permitted.
Related errors
- Calling the test function inside another test function is…
- Browser is not initialized
- Browser provider is not defined for the project
- Cannot annotate tests outside of the test run. The test
- Cannot call "onTestFailed" inside a test hook.
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/07f8a6bc2b7462bd.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/runtime/runner/suite.ts:600
.map(r => (r.status === 'rejected' ? r.reason : undefined))
.filter(Boolean)
if (errors.length) {
throw errors
}
}
return fnResult
}) as T
}
function createSuite() {
function suiteFn(
this: Record<string, boolean | undefined>,
name: string | Function,
factoryOrOptions?: SuiteFactory | SuiteOptions,
optionsOrFactory?: number | SuiteFactory,
) {
if (getCurrentTest()) {
throw new Error(
'Calling the suite function inside test function is not allowed. It can be only called at the top level or inside another suite function.',
)
}
const currentSuite: SuiteCollector | undefined = collectorContext.currentSuite || defaultSuite
let { options, handler: factory } = parseArguments(
factoryOrOptions,
optionsOrFactory,
) as { options: SuiteOptions; handler: SuiteFactory | undefined }
const { meta: parentMeta, ...parentOptions } = currentSuite?.options || {}
// inherit options from current suite
options = {
...parentOptions,
...options,
}
View on GitHub (pinned to 1fa9837ec2)