prefer-hooks-on-top
Added in v0.6.3Configuration
Rule Details
Suggest placing Jest lifecycle hooks before any test cases in the same scope.
Hooks (beforeAll, beforeEach, afterEach, afterAll) can appear anywhere in a
describe callback, but Jest always runs them in a fixed order regardless of where
they are written. Mixing hooks with test / it calls makes that execution order
harder to follow, so this rule reports hooks registered after the first test case
in the same scope.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Differences from upstream
Every function body is a scope of its own, where upstream only opens a new scope at a
call expression. A hook written in a function that is never called registers nothing at
run time, and a function passed to describe by name is that suite's body rather than a
continuation of the code around it, so neither inherits the test cases registered
outside it:
The other side of that boundary is that a hook registered through a helper the suite does call is judged against the helper instead of the suite, and so goes unreported.