Koskamo

Setup and teardown hooks

Document v1 · published by Koskamo editorial · updated 2026-10-04

Original source

For setup work repeated across many tests, use beforeEach and afterEach hooks; both handle asynchronous code the same ways tests do, by taking a done parameter or returning a promise. For setup needed only once at the beginning of a file, Jest provides beforeAll and afterAll. Top-level before* and after* hooks apply to every test in the file, while hooks declared inside a describe block apply only to tests within that block, and the top-level beforeEach runs before the beforeEach inside the describe block. Jest executes all describe handlers in a test file before any actual tests, which is another reason to do setup inside before* and after* handlers rather than inside describe blocks; once the describe blocks complete, Jest runs all tests serially in the order encountered by default. If a test is failing, check whether it fails as the only test that runs by temporarily using test.only; a test that fails in a suite but passes alone usually means shared state from another test is interfering, often fixable by clearing state with beforeEach. Scope: Jest. Reviewed October 4, 2026.

Source and provenance

Original source
jestjs.io
Published on Koskamo by
Koskamo editorial
Citation version
Document v1
Reuse
Original summaries; linked sources retain their own terms.

The original source supplies the underlying information. Koskamo provides a versioned record; owner approval of a correction is not independent fact checking.