vitest-dev/vitest · error · Error

Calling the test function inside another test function is no

Error message

Calling the test function inside another test function is not allowed. Please put it inside "describe" or "suite" so it can be properly collected.

What it means

The `test`/`it` function checks `getCurrentTest()` at call time (suite.ts:113-117). If a test is defined while another test is already running, Vitest cannot collect it (collection happens before execution, not during), so it throws. Tests must be declared at the top level or inside `describe`/`suite` blocks — never nested inside another test's body.

Source

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

 * test('should add two numbers', () => {
 *   expect(add(1, 2)).toBe(3);
 * });
 * ```
 * @example
 * ```ts
 * // Define a test with options
 * test('should subtract two numbers', { retry: 3 }, () => {
 *   expect(subtract(5, 2)).toBe(3);
 * });
 * ```
 */
export const test: TestAPI = createTest(function (
  name: string | Function,
  optionsOrFn?: TestOptions | TestFunction,
  optionsOrTest?: number | TestOptions | TestFunction,
) {
  if (getCurrentTest()) {
    throw new Error(
      'Calling the test function inside another test function is not allowed. Please put it inside "describe" or "suite" so it can be properly collected.',
    )
  }

  getCurrentSuite().test.fn.call(
    this,
    formatName(name),
    optionsOrFn as TestOptions,
    optionsOrTest as TestFunction,
  )
})

/**
 * Creates a suite of tests, allowing for grouping and hierarchical organization of tests.
 * Suites can contain both tests and other suites, enabling complex test structures.
 *
 * @param {string} name - The name of the suite, used for identification and reporting.
 * @param {Function} fn - A function that defines the tests and suites within this suite.

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Move nested test declarations out to the `describe`/`suite` level.
  2. Use `test.each` or a loop inside `describe` for parameterized cases instead of defining tests at runtime.
  3. Restructure helpers to perform assertions, not test registration.

Example fix

// before
test('outer', () => {
  test('inner', () => { expect(true).toBe(true) }) // throws
})
// after
describe('group', () => {
  test('outer', () => {})
  test('inner', () => { expect(true).toBe(true) })
})
Defensive patterns

Strategy: validation

Validate before calling

// Structural: ensure test()/it() are not lexically nested in another test body.
// ESLint: ban CallExpression callees 'test'/'it' inside other test callbacks.

Prevention

When it happens

Trigger: Writing `test('outer', () => { test('inner', () => {}) })`; dynamically calling `test(...)` from within a running test; helper functions invoked during a test that define tests.

Common situations: Porting nested test structures from frameworks that allow it; helper functions that accidentally define tests; code that iterates and calls `test` inside a test body.

Related errors


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