jestjs/jest · error · Error
Tests cannot be nested. Test "${spec.description}" cannot ru
Error message
Tests cannot be nested. Test "${spec.description}" cannot run because it is nested within "${currentSpec.description}". What it means
Jasmine does not support defining a test (it) inside another test (it). Env.ts:576-580 throws when currentSpec is not null at the time it() is called, meaning execution is already inside a spec body. The message names both the inner and outer specs so you can locate the nesting. Defining tests at runtime would also produce nondeterministic suites, so it is rejected up front.
Source
Thrown at packages/jest-jasmine2/src/jasmine/Env.ts:577
if (typeof fn !== 'function') {
throw new TypeError(
`Invalid second argument, ${fn}. It must be a callback function.`,
);
}
const spec = specFactory(
description,
fn,
currentDeclarationSuite,
timeout,
);
if (currentDeclarationSuite.markedPending) {
spec.pend();
}
// When a test is defined inside another, jasmine will not run it.
// This check throws an error to warn the user about the edge-case.
if (currentSpec !== null) {
throw new Error(
`Tests cannot be nested. Test "${spec.description}" cannot run because it is nested within "${currentSpec.description}".`,
);
}
currentDeclarationSuite.addChild(spec);
return spec;
};
this.xit = function (...args) {
const spec = this.it.apply(this, args);
spec.pend('Temporarily disabled with xit');
return spec;
};
this.todo = function () {
const description = arguments[0];
if (arguments.length !== 1 || typeof description !== 'string') {
throw new ErrorWithStack(
'Todo must be called with only a description.',View on GitHub (pinned to f49721c78e)
Solutions
- Move the inner it() up to the describe level so both tests are siblings, not nested.
- If you need data-driven sub-assertions inside one test, use multiple expect() calls or it.each at the describe level.
- Use the two spec names in the message to jump to the offending lines and un-indent the inner test.
Example fix
// before — nested it
describe('calc', () => {
it('adds', () => {
it('subtracts', () => { expect(1-1).toBe(0); }); // throws
expect(1+1).toBe(2);
});
});
// after — sibling tests
describe('calc', () => {
it('adds', () => { expect(1+1).toBe(2); });
it('subtracts', () => { expect(1-1).toBe(0); });
}); Defensive patterns
Strategy: validation
Validate before calling
// Static guard: it/test must not appear inside another it/test body.
function nestedIt(source: string): boolean {
return /\bit\s*\([^)]*\)\s*=>\s*{[^}]*\bit\s*\(/.test(source);
} Prevention
- Keep all it/test definitions directly inside describe blocks, never inside another it.
- Use it.each at the describe level for data-driven sub-cases.
When it happens
Trigger: Writing `it('outer', () => { it('inner', () => {}); })`. currentSpec is set in specStarted (Env.ts:546) and cleared in specResultCallback (line 541); during the outer spec's body it is non-null, so the nested it() throws at line 577.
Common situations: Refactoring a describe and accidentally nesting an it inside another it; loop that calls it() inside a test body; conditional test definition that ended up scoped to a spec.
Related errors
- Missing second argument. It must be a callback function. Per
- Invalid second argument, ${fn}. It must be a callback functi
- Todo must be called with only a description.
- Missing second argument. It must be a callback function.
- Invalid second argument, ${specDefinitions}. It must be a ca
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/fdf135df1aa18f03.json.
Report an issue: GitHub.