beforeEach Run
Predict Output Easy
Question
What does this test code print?
Code
let count = 0;
beforeEach(() => { count = 0; });
test("a", () => { count++; expect(count).toBe(1); });
test("b", () => { count++; expect(count).toBe(1); });
// Both tests?
Hint
beforeEach resets count to 0 before each test.
Explanation
beforeEach runs before each test, resetting count to 0. In test "a", count becomes 1. In test "b", count is reset to 0 then becomes 1. Both tests pass.
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.