noTestHooks (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noTestHooks - This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Same as
jest/no-hooks - Same as
vitest/no-hooks - Same as
playwright/no-hooks
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noTestHooks": "error" } } }}Description
Section titled “Description”Disallow setup and teardown hooks in tests.
Test frameworks provide hooks, which are functions that run code before or after tests:
beforeAll, beforeEach, afterEach and afterAll, and the before and after hooks of node:test and Mocha.
Hooks usually prepare variables that several tests share. To understand one test, the reader has to find every hook that runs around it. A test can also pass or fail depending on what an earlier test left in the shared variables. Calling the setup code inside each test keeps the test complete on its own.
The rule reports a hook that is called as a function, such as beforeEach(), when the hook is
a global function or when the file loads it from @jest/globals, bun:test, mocha, node:test or vitest.
A hook that the file loads under another name is reported too.
A function that has the name of a hook but comes from anywhere else, such as a function
that the file declares, is not reported.
The rule also reports beforeAll, beforeEach, afterEach and afterAll when they are called
on test, it, describe or suite, such as test.beforeEach().
Examples
Section titled “Examples”Invalid
Section titled “Invalid”describe("cart", () => { let cart;
beforeEach(() => { cart = createCart(); });
it("starts empty", () => { expect(cart.items).toHaveLength(0); });});code-block.js:4:5 lint/nursery/noTestHooks ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The beforeEach hook is not allowed.
2 │ let cart;
3 │
> 4 │ beforeEach(() => {
│ ^^^^^^^^^^
5 │ cart = createCart();
6 │ });
ℹ Hooks run code away from the tests that depend on it, and they encourage tests to share variables. This makes a test harder to understand on its own, and lets one test change the result of another.
ℹ Move this code into the tests that need it, for example by calling a helper function at the start of each test.
ℹ 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.
test.afterAll(async () => { await server.close();});code-block.js:1:1 lint/nursery/noTestHooks ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The afterAll hook is not allowed.
> 1 │ test.afterAll(async () => {
│ ^^^^^^^^^^^^^
2 │ await server.close();
3 │ });
ℹ Hooks run code away from the tests that depend on it, and they encourage tests to share variables. This makes a test harder to understand on its own, and lets one test change the result of another.
ℹ Move this code into the tests that need it, for example by calling a helper function at the start of each test.
ℹ 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("cart", () => { it("starts empty", () => { const cart = createCart(); expect(cart.items).toHaveLength(0); });});
// This function is declared in the file, so it isn't a hook.function before(message) { console.log(message);}before("starting");Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.