Skip to content

useTopLevelDescribe (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useTopLevelDescribe": "error"
}
}
}
}

Require tests and hooks to be inside a describe block.

A describe block gives a group of tests a shared name and keeps their hooks next to them. A hook is a function such as beforeEach or afterAll that runs before or after tests. Tests and hooks written outside of any describe block have no such group, which makes a test file harder to navigate as it grows.

This rule reports calls that declare a test, such as test and it, and calls that declare a hook, when they are not inside a describe block. It can also limit how many describe blocks a file has at its top level.

Tests and hooks are recognized by the name of the function, such as test, it, beforeEach, or after. The rule doesn’t check where that function comes from, so an unrelated function with one of these names is reported as well.

test("adds two numbers", () => {});
code-block.js:1:1 lint/nursery/useTopLevelDescribe ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This test is not inside a describe block.

> 1 │ test("adds two numbers", () => {});
│ ^^^^
2 │

ℹ A test outside of a describe block isn't grouped with related tests under a shared name, which makes the file harder to navigate as it grows.

ℹ Move this test into a describe block.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

beforeEach(() => {});
describe("calculator", () => {
test("adds two numbers", () => {});
});
code-block.js:1:1 lint/nursery/useTopLevelDescribe ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This hook is not inside a describe block.

> 1 │ beforeEach(() => {});
│ ^^^^^^^^^^
2 │
3 │ describe("calculator", () => {

ℹ A hook outside of a describe block isn't tied to a named group of tests, which makes it harder to tell which tests depend on it.

ℹ Move this hook into the describe block that contains the tests it applies to.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

describe("calculator", () => {
beforeEach(() => {});
test("adds two numbers", () => {});
describe("division", () => {
test("divides two numbers", () => {});
});
});

Default: no limit

The maximum number of describe blocks allowed at the top level of a file. A describe block that is inside another describe block doesn’t count towards this limit.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useTopLevelDescribe": {
"level": "on",
"options": {
"maxTopLevelDescribes": 1
}
}
}
}
}
}
describe("addition", () => {
test("adds two numbers", () => {});
});
describe("division", () => {
test("divides two numbers", () => {});
});
code-block.js:5:1 lint/nursery/useTopLevelDescribe ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This top-level describe block exceeds the maximum of 1 allowed in a file.

3 │ });
4 │
> 5 │ describe("division", () => {
│ ^^^^^^^^
6 │ test("divides two numbers", () => {});
7 │ });

ℹ A file with many top-level describe blocks is harder to navigate than a file that groups them under a shared name.

ℹ Move this block into another describe block, or move it to a separate file.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

describe("calculator", () => {
describe("addition", () => {
test("adds two numbers", () => {});
});
describe("division", () => {
test("divides two numbers", () => {});
});
});