{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/runner/suite.ts#L96-L132","documentation":"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.","triggerScenarios":"Writing `test('outer', () => { test('inner', () => {}) })`; dynamically calling `test(...)` from within a running test; helper functions invoked during a test that define tests.","commonSituations":"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.","solutions":["Move nested test declarations out to the `describe`/`suite` level.","Use `test.each` or a loop inside `describe` for parameterized cases instead of defining tests at runtime.","Restructure helpers to perform assertions, not test registration."],"exampleFix":"// before\ntest('outer', () => {\n  test('inner', () => { expect(true).toBe(true) }) // throws\n})\n// after\ndescribe('group', () => {\n  test('outer', () => {})\n  test('inner', () => { expect(true).toBe(true) })\n})","handlingStrategy":"validation","validationCode":"// Structural: ensure test()/it() are not lexically nested in another test body.\n// ESLint: ban CallExpression callees 'test'/'it' inside other test callbacks.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare tests only at top level or inside describe/suite.","Use test.each for parameterized cases rather than runtime loops defining tests.","Refactor helpers to assert, not register tests."],"tags":["test-definition","nesting","collection","lifecycle"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}