{"record":{"id":"07f8a6bc2b7462bd","repo":"vitest-dev/vitest","slug":"calling-the-suite-function-inside-test-function-is","errorCode":null,"errorMessage":"Calling the suite function inside test function is not allowed. It can be only called at the top level or inside another suite function.","messagePattern":"Calling the suite function inside test function is not allowed\\. It can be only called at the top level or inside another suite function\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/runner/suite.ts","lineNumber":600,"sourceCode":"        .map(r => (r.status === 'rejected' ? r.reason : undefined))\n        .filter(Boolean)\n      if (errors.length) {\n        throw errors\n      }\n    }\n    return fnResult\n  }) as T\n}\n\nfunction createSuite() {\n  function suiteFn(\n    this: Record<string, boolean | undefined>,\n    name: string | Function,\n    factoryOrOptions?: SuiteFactory | SuiteOptions,\n    optionsOrFactory?: number | SuiteFactory,\n  ) {\n    if (getCurrentTest()) {\n      throw new Error(\n        'Calling the suite function inside test function is not allowed. It can be only called at the top level or inside another suite function.',\n      )\n    }\n\n    const currentSuite: SuiteCollector | undefined = collectorContext.currentSuite || defaultSuite\n\n    let { options, handler: factory } = parseArguments(\n      factoryOrOptions,\n      optionsOrFactory,\n    ) as { options: SuiteOptions; handler: SuiteFactory | undefined }\n\n    const { meta: parentMeta, ...parentOptions } = currentSuite?.options || {}\n    // inherit options from current suite\n    options = {\n      ...parentOptions,\n      ...options,\n    }\n","sourceCodeStart":582,"sourceCodeEnd":618,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/runner/suite.ts#L582-L618","documentation":"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().","triggerScenarios":"Writing `test('x', () => { describe('inner', () => {...}) })`; calling a helper that calls describe() from within a test; generating suites conditionally at runtime.","commonSituations":"Converting static config into runtime-generated suites; helper functions that wrap describe and are invoked from tests; framework migration where runtime nesting was permitted.","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."],"exampleFix":"// before\ntest('x', () => {\n  describe('group', () => { /* ... */ }) // throws\n})\n\n// after\ndescribe('group', () => {\n  test('x', () => {})\n})","handlingStrategy":"validation","validationCode":"import { getCurrentTest } from 'vitest'\nfunction defineSuite(name, factory) {\n  if (getCurrentTest()) throw new Error('describe() cannot be called inside a test')\n  return describe(name, factory)\n}","typeGuard":"const atSuiteScope = () => !getCurrentTest()","tryCatchPattern":null,"preventionTips":["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."],"tags":["suite","collector","lifecycle"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}