{"record":{"id":"a2ea7ea63bb79541","repo":"oxc-project/oxc","slug":"do-not-use-setup-or-teardown-hooks","errorCode":null,"errorMessage":"Do not use setup or teardown hooks.","messagePattern":"Do not use setup or teardown hooks\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/no_hooks.rs","lineNumber":15,"sourceCode":"use schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_span::{GetSpan, Span};\nuse oxc_str::CompactStr;\n\nuse crate::{\n    context::LintContext,\n    utils::{JestFnKind, JestGeneralFnKind, PossibleJestNode, is_type_of_jest_fn_call},\n};\n\nfn unexpected_hook_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not use setup or teardown hooks.\")\n        .with_help(\"Inline the setup or teardown logic directly in each test for better readability and isolation.\")\n        .with_label(span)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nDisallows Jest setup and teardown hooks, such as `beforeAll`.\n\n### Why is this bad?\n\nJest provides global functions for setup and teardown tasks, which are\ncalled before/after each test case and each test suite. The use of these\nhooks promotes shared state between tests.\n\nThis rule reports for the following function calls:\n* `beforeAll`\n* `beforeEach`\n* `afterAll`","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/no_hooks.rs#L1-L33","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inline the hook's setup/teardown logic into each test (or into a helper the test calls explicitly)","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'])","Disable the rule for transition files with an oxlint-disable comment while migrating incrementally"],"exampleFix":"// before\nbeforeEach(() => { resetDb(); });\nit('lists users', () => { expect(list()).toEqual([]); });\n\n// after\nit('lists users', () => {\n  resetDb();\n  expect(list()).toEqual([]);\n});","handlingStrategy":"validation","validationCode":"rg -n \"\\b(?:beforeAll|beforeEach|afterAll|afterEach)\\(\" tests/ # preview hooks that the rule will flag","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide the team policy first: inline setup vs hooks; configure `allow` for permitted hook types","Migrate suites file-by-file with oxlint-disable scoped comments instead of enabling repo-wide at once","Extract setup into plain helper functions tests call explicitly"],"tags":["jest","vitest","oxlint","testing","hooks","lifecycle","style"],"backgroundTag":"test-lifecycle-hooks","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}