vitest-dev/vitest · error · Error

Calling the suite function inside test function is not allow

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

The `suite`/`describe` factory function checks `getCurrentTest()` at call time (suite.ts:592-596). Defining a suite inside a running test is disallowed because suite collection occurs before test execution — a suite declared mid-test could never be collected. Suites may only be declared at the top level or nested inside other suites.

Source

Thrown at packages/vitest/src/runtime/runner/suite.ts:593

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

Solutions

  1. Move `describe`/`suite` declarations to the top level or inside another `describe`.
  2. Use `test.each`/`describe.each` for parameterized grouping instead of runtime suite creation.
  3. If logic must be conditional, use `describe.skipIf`/`test.runIf` at the suite level.

Example fix

// before
test('setup', () => {
  describe('group', () => { // throws
    test('a', () => {})
  })
})
// after
describe('group', () => {
  test('setup', () => {})
  test('a', () => {})
})
Defensive patterns

Strategy: validation

Validate before calling

// Structural: ensure describe()/suite() are not lexically nested inside test() bodies.
// ESLint: ban CallExpression callees 'describe'/'suite' inside test callbacks.

Prevention

When it happens

Trigger: Writing `test('x', () => { describe('inner', () => { ... }) })`; calling `suite(...)` from within a test body or a function invoked during a test; dynamic suite creation at runtime.

Common situations: Porting from frameworks that permit runtime suite definition; helper functions that wrap `describe`; conditionally building suites inside tests.

Related errors


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