{"record":{"id":"fdf135df1aa18f03","repo":"jestjs/jest","slug":"tests-cannot-be-nested-test-spec-description","errorCode":null,"errorMessage":"Tests cannot be nested. Test \"${spec.description}\" cannot run because it is nested within \"${currentSpec.description}\".","messagePattern":"Tests cannot be nested\\. Test \"(.+?)\" cannot run because it is nested within \"(.+?)\"\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-jasmine2/src/jasmine/Env.ts","lineNumber":585,"sourceCode":"        if (typeof fn !== 'function') {\n          throw new TypeError(\n            `Invalid second argument, ${fn}. It must be a callback function.`,\n          );\n        }\n        const spec = specFactory(\n          description,\n          fn,\n          currentDeclarationSuite,\n          timeout,\n        );\n        if (currentDeclarationSuite.markedPending) {\n          spec.pend();\n        }\n\n        // When a test is defined inside another, jasmine will not run it.\n        // This check throws an error to warn the user about the edge-case.\n        if (currentSpec !== null) {\n          throw new Error(\n            `Tests cannot be nested. Test \"${spec.description}\" cannot run because it is nested within \"${currentSpec.description}\".`,\n          );\n        }\n        currentDeclarationSuite.addChild(spec);\n        return spec;\n      };\n\n      this.xit = function (...args) {\n        const spec = this.it.apply(this, args);\n        spec.pend('Temporarily disabled with xit');\n        return spec;\n      };\n\n      this.todo = function () {\n        const description = arguments[0];\n        if (arguments.length !== 1 || typeof description !== 'string') {\n          throw new ErrorWithStack(\n            'Todo must be called with only a description.',","sourceCodeStart":567,"sourceCodeEnd":603,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-jasmine2/src/jasmine/Env.ts#L567-L603","documentation":"Jasmine builds the test tree during the describe phase, then executes specs. Calling `it(...)` inside another spec's body (an `it` callback) means the nested spec is created DURING execution, when `currentSpec` is already set — jasmine cannot run it and would silently skip it. Env.it detects `currentSpec !== null` at line 584 and throws this Error naming both the nested and the outer spec to make the misuse explicit.","triggerScenarios":"Writing `it('outer', () => { it('inner', () => {}) })`. The check `currentSpec !== null` at Env.ts:584 is true because the outer spec is executing. The error includes both `spec.description` (inner) and `currentSpec.description` (outer).","commonSituations":"Coming from Mocha which historically allowed (then deprecated) nested tests; a helper that registers specs and is accidentally called from within a spec body rather than a describe body; a loop inside a spec that tries to generate cases dynamically.","solutions":["Move the nested `it` out to the describe level so it is a sibling, not a child of the other spec.","For dynamic case generation, use `describe.each` or generate the cases inside a `describe` (not an `it`).","If you need sub-assertions inside a test, use multiple `expect()` calls rather than nested `it`."],"exampleFix":"// before — nested it, throws at runtime\nit('outer', () => {\n  it('inner', () => { expect(1).toBe(1); });\n});\n// after — siblings\ndescribe('outer', () => {\n  it('inner', () => { expect(1).toBe(1); });\n});","handlingStrategy":"validation","validationCode":"// Static lint: forbid it() calls inside it() bodies.\n// Use eslint-plugin-jest rule: 'no-nested-tests' or a custom visitor\n// that errors when an `it`/`test` CallExpression appears inside another\n// `it`/`test` CallExpression's callback.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Register tests at the describe level, never inside an it body.","Use describe.each or it.each for data-driven cases rather than loops in specs.","Add an ESLint rule (eslint-plugin-jest) to catch nested test definitions."],"tags":["it","nesting","jasmine","test-structure","api-misuse"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}