{"record":{"id":"98997657cac81d91","repo":"oxc-project/oxc","slug":"require-setup-and-teardown-code-to-be-within-a-hoo","errorCode":null,"errorMessage":"Require setup and teardown code to be within a hook.","messagePattern":"Require setup and teardown code to be within a hook\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/require_hook.rs","lineNumber":22,"sourceCode":"    ast::{Argument, Expression, Statement, VariableDeclarationKind},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_semantic::AstNode;\nuse oxc_span::Span;\nuse oxc_str::CompactStr;\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    context::LintContext,\n    utils::{\n        JestFnKind, JestGeneralFnKind, PossibleJestNode, get_node_name, is_type_of_jest_fn_call,\n        valid_vitest_fn::is_valid_vitest_call,\n    },\n};\n\nfn use_hook(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Require setup and teardown code to be within a hook.\")\n        .with_help(\"This should be done within a hook\")\n        .with_label(span)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nThis rule flags any expression that is either at the toplevel of a test file or\ndirectly within the body of a `describe`, _except_ for the following:\n\n- `import` statements\n- `const` variables\n- `let` _declarations_, and initializations to `null` or `undefined`\n- Classes\n- Types\n- Calls to the standard Jest globals\n\n### Why is this bad?\n","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/require_hook.rs#L4-L40","documentation":"Warning from the oxlint `jest/require-hook` rule (source: crates/oxc_linter/src/rules/shared/jest_vitest/require_hook.rs:22). It flags setup/teardown statements sitting at the top level of a test file or directly inside a `describe` body, requiring them to live in `beforeAll`/`beforeEach`/`afterAll`/`afterEach`. The rule has a documented allow-list: imports, `const` variables, `let` declarations initialized to null/undefined, and valid vitest calls (`is_valid_vitest_call` in the source).","triggerScenarios":"Statements such as `jest.resetAllMocks()`, `mockFn.mockClear()`, assignments like `process.env.NODE_ENV = 'test'`, or any non-declaration expression at the top level of a spec file or directly in a `describe` callback (but not inside `test` callbacks).","commonSituations":"Shared setup accidentally drifting to file top level when tests are copy-pasted; environment mutation leaking between test files because it is not in a hook; teams adopting the rule mid-project and getting many hits at once.","solutions":["Move the statement into the appropriate hook: `beforeEach(() => { jest.resetAllMocks(); })` for per-test setup, `beforeAll` for one-time setup.","If the statement is a constant, declare it with `const` (const declarations are allowed at top level by the rule).","For vitest-specific top-level calls, check whether the call is in the rule's allowed vitest list; if it is a legitimate framework call, update oxlint so the allow-list applies.","Suppress a known-safe case with `// oxlint-disable-next-line jest/require-hook` or tune the rule's allowed-function-name options in `.oxlintrc.json`."],"exampleFix":"// before\njest.resetAllMocks();\ndescribe('api', () => {\n  test('lists users', () => { /* ... */ });\n});\n\n// after\ndescribe('api', () => {\n  beforeEach(() => {\n    jest.resetAllMocks();\n  });\n  test('lists users', () => { /* ... */ });\n});","handlingStrategy":"validation","validationCode":"// rg -n \"^(jest\\.|vi\\.|process\\.env)\" tests/ -t ts | rg -v \"(before|after)(Each|All)\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep all setup inside beforeEach/afterEach hooks from the first day of a suite","Know the rule's allow-list (imports, const, let to null/undefined) and use const for shared fixtures","Run oxlint locally with the jest plugin before pushing, not only in CI"],"tags":["jest","vitest","oxlint","hooks","test-structure"],"backgroundTag":"jest-setup-teardown-hook","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}