Linter · Rules
jest/no-duplicate-hooks
Disallow using the same hook twice in one describe block
Two beforeEach calls in the same block both run, so nothing fails. But someone who reads the first one assumes that's all the setup and misses the second, which can be many lines below.
Hooks are counted per describe block, and the top level of the file counts as a block. A nested describe can have its own beforeEach.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/no-duplicate-hooks" = "error" # or "warn"
Examples
Reported:
import { beforeEach, describe, test } from "@jest/globals";
describe("cart", () => {
beforeEach(() => {
cart.clear();
});
test("starts empty", () => {});
beforeEach(() => {
cart.add("apple");
});
});
Not reported:
import { beforeEach, describe, test } from "@jest/globals";
describe("cart", () => {
beforeEach(() => {
cart.clear();
cart.add("apple");
});
test("has one item", () => {});
describe("after checkout", () => {
beforeEach(() => {
cart.checkout();
});
test("is empty", () => {});
});
});