Available hooks
beforeAll
Runs once before all tests in adescribe block:
beforeEach
Runs before each test:afterEach
Runs after each test:afterAll
Runs once after all tests in adescribe block:
Async hooks
All hooks support async operations:Hook execution order
Hooks execute in the following order:beforeAll(outer describe)beforeAll(inner describe)beforeEach(outer describe)beforeEach(inner describe)- test
afterEach(inner describe)afterEach(outer describe)afterAll(inner describe)afterAll(outer describe)
Scoping
Hooks are scoped to thedescribe block they’re defined in:
Timeout
Hooks can have custom timeouts:Done callback
Hooks support the done callback pattern:Prefer using async/await over done callbacks when possible.
Error handling
If a hook throws an error:beforeAll/beforeEach: The test is marked as failedafterEach/afterAll: The test result is preserved, but the error is logged
Resource cleanup with using
Bun supports JavaScript’susing keyword for automatic resource cleanup: