require-hook

July 2, 2026 ยท View on GitHub

๐Ÿ“ Require setup and teardown code to be inside a hook.

๐Ÿšซ This rule is disabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

Code placed directly at the top level of a test file or inside a describe body runs when the file is loaded (while tests are being collected), not as part of any test. Setup written there runs once, in collection order, before any test or hook โ€” a common source of surprising, order-dependent failures. Putting setup and teardown in a before/beforeEach/after/afterEach hook makes the timing explicit and lets the runner manage it per test or per suite.

This rule reports bare function calls at the module top level or directly inside a describe/suite body. The test, suite, and hook registration calls themselves are allowed, as are assertions (reported by no-assert-in-describe) and variable declarations. Use the allow option to permit specific calls.

Options

allow

Type: string[]
Default: []

A list of callee expressions to allow at the top level, for example ['console.log'].

Examples

import test, {beforeEach} from 'node:test';

// โŒ
startServer();

test('title', () => {});

// โœ…
beforeEach(() => {
	startServer();
});

test('title', () => {});