oxc-project/oxc · warning · OxcDiagnostic

Do not use setup or teardown hooks.

Error message

Do not use setup or teardown hooks.

What it means

Diagnostic from oxlint rule `jest/no-hooks` (shared with vitest), the unexpected_hook_diagnostic in crates/oxc_linter/src/rules/shared/jest_vitest/no_hooks.rs:20. It flags every use of the setup/teardown hooks beforeAll, beforeEach, afterAll and afterEach (detected via is_type_of_jest_fn_call), encouraging setup logic to be inlined directly in each test for readability and isolation.

Source

Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/no_hooks.rs:15

use schemars::JsonSchema;
use serde::Deserialize;

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{GetSpan, Span};
use oxc_str::CompactStr;

use crate::{
    context::LintContext,
    utils::{JestFnKind, JestGeneralFnKind, PossibleJestNode, is_type_of_jest_fn_call},
};

fn unexpected_hook_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Do not use setup or teardown hooks.")
        .with_help("Inline the setup or teardown logic directly in each test for better readability and isolation.")
        .with_label(span)
}

pub const DOCUMENTATION: &str = r"### What it does

Disallows Jest setup and teardown hooks, such as `beforeAll`.

### Why is this bad?

Jest provides global functions for setup and teardown tasks, which are
called before/after each test case and each test suite. The use of these
hooks promotes shared state between tests.

This rule reports for the following function calls:
* `beforeAll`
* `beforeEach`
* `afterAll`

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Inline the hook's setup/teardown logic into each test (or into a helper the test calls explicitly)
  2. Allow specific hooks via configuration (the rule supports an `allow` list in the ESLint jest plugin; set the equivalent option in .oxlintrc.json, e.g. allow: ['afterEach'])
  3. Disable the rule for transition files with an oxlint-disable comment while migrating incrementally

Example fix

// before
beforeEach(() => { resetDb(); });
it('lists users', () => { expect(list()).toEqual([]); });

// after
it('lists users', () => {
  resetDb();
  expect(list()).toEqual([]);
});
Defensive patterns

Strategy: validation

Validate before calling

rg -n "\b(?:beforeAll|beforeEach|afterAll|afterEach)\(" tests/ # preview hooks that the rule will flag

Prevention

When it happens

Trigger: Any call classified as a jest general hook kind inside a test file: beforeAll(...), beforeEach(...), afterAll(...), afterEach(...). Each occurrence gets its own diagnostic on the call span.

Common situations: Teams adopting the 'setup inside the test' style (e.g. React Testing Library guidance) and enabling this rule on legacy suites; opinionated codebases banning shared mutable setup; confusion when the rule is enabled repo-wide and existing suites rely heavily on hooks.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/a2ea7ea63bb79541. Report an issue: GitHub.