{"record":{"id":"8fa824533168e56f","repo":"vitest-dev/vitest","slug":"calling-the-test-function-inside-another-test-func","errorCode":null,"errorMessage":"Calling the test function inside another test function is not allowed. Please put it inside \"describe\" or \"suite\" so it can be properly collected.","messagePattern":"Calling the test function inside another test function is not allowed\\. Please put it inside \"describe\" or \"suite\" so it can be properly collected\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/runner/suite.ts","lineNumber":114,"sourceCode":" * test('should add two numbers', () => {\n *   expect(add(1, 2)).toBe(3);\n * });\n * ```\n * @example\n * ```ts\n * // Define a test with options\n * test('should subtract two numbers', { retry: 3 }, () => {\n *   expect(subtract(5, 2)).toBe(3);\n * });\n * ```\n */\nexport const test: TestAPI = createTest(function (\n  name: string | Function,\n  optionsOrFn?: TestOptions | TestFunction,\n  optionsOrTest?: number | TestOptions | TestFunction,\n) {\n  if (getCurrentTest()) {\n    throw new Error(\n      'Calling the test function inside another test function is not allowed. Please put it inside \"describe\" or \"suite\" so it can be properly collected.',\n    )\n  }\n\n  getCurrentSuite().test.fn.call(\n    this,\n    formatName(name),\n    optionsOrFn as TestOptions,\n    optionsOrTest as TestFunction,\n  )\n})\n\n/**\n * Creates a suite of tests, allowing for grouping and hierarchical organization of tests.\n * Suites can contain both tests and other suites, enabling complex test structures.\n *\n * @param {string} name - The name of the suite, used for identification and reporting.\n * @param {Function} fn - A function that defines the tests and suites within this suite.","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/runner/suite.ts#L96-L132","documentation":"Thrown by the test() API when getCurrentTest() returns a truthy value, i.e., test() is invoked while another test's body is executing. Vitest collects tests statically (at describe/suite scope); nested runtime calls would produce tasks after collection closed, so they are rejected. Tests must live at the top level or inside describe/suite factories.","triggerScenarios":"Writing `test('outer', () => { test('inner', () => {}) })`; calling a helper that itself calls test() from within a test body; conditionally defining tests at runtime inside a test.","commonSituations":"Migrating from frameworks that allow runtime test definition; generating tests dynamically inside a loop that runs during a test instead of during collection; helper functions misused as test bodies.","solutions":["Move nested test() calls out to the describe/suite or top level.","Generate dynamic tests at module top-level or inside describe, not inside test bodies.","For data-driven tests, use it.each / test.each at collection time."],"exampleFix":"// before\ntest('outer', () => {\n  test('inner', () => {}) // throws\n})\n\n// after\ndescribe('group', () => {\n  test('outer', () => {})\n  test('inner', () => {})\n})","handlingStrategy":"validation","validationCode":"import { getCurrentTest } from 'vitest'\nfunction defineTest(name: string, fn: () => void) {\n  if (getCurrentTest()) throw new Error('do not nest test() calls')\n  return test(name, fn)\n}","typeGuard":"const atTopLevel = () => !getCurrentTest()","tryCatchPattern":null,"preventionTips":["Define tests at the top level or inside describe only.","Generate dynamic tests at collection time with it.each, not at runtime.","Review helpers that wrap test() to ensure they are not called from test bodies."],"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"}