beforeEach Purpose
Question
What does beforeEach do?
Hint
beforeEach resets state between tests for isolation.
Explanation
beforeEach(fn) runs the setup function before each test in the current describe block (and nested blocks). It is used to reset state, create fresh objects, or set up common preconditions. Example: beforeEach(() => { user = new User(); }) ensures each test gets a fresh user. This prevents tests from affecting each other (test isolation). afterEach runs after each test for cleanup. beforeAll/afterAll run once for the entire block (use sparingly — less isolation). Prefer beforeEach for better isolation.
Related Resources
Related Lessons
- Setup/Teardown
Learn setup.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.