afterEach Function

All Jest topics
∙ Jest

afterEach Function focuses on cleanup executed after every test in a scope. It uses `afterEach()` to confirm mocks, timers, files, connections, or state restored after each test.

📝Syntax
afterEach(() => { jest.restoreAllMocks(); })
aftereach-function.test.js
📝 Jest Example
👁 Expected Result
💡 Run the test from isolated state and read the matcher diff when it fails.
👀Output
afterEach Function: pASS — uses a spy
🔍Line-by-Line Explanation
LineMeaning
afterEach(() => {In afterEach Function, line 2 implements setup, action, or verification for this example.
jest.restoreAllMocks();In afterEach Function, line 3 implements setup, action, or verification for this example.
});In afterEach Function, line 4 implements setup, action, or verification for this example.
test('uses a spy', () => {In afterEach Function, line 5 declares a named Jest test.
expect(true).toBe(true);In afterEach Function, line 6 creates an expectation for the received value.
});In afterEach Function, line 7 implements setup, action, or verification for this example.
🌐Real-World Uses
  • 1Use afterEach Function to verify cleanup executed after every test in a scope.
  • 2afterEach Function is valuable in unit-testing fundamentals when the test must prove mocks, timers, files, connections, or state restored after each test.
  • 3A useful failure record for afterEach Function contains cleanup errors attached to the responsible test.
Common Mistakes
  • 1afterEach Function commonly fails because of cleanup failures being hidden or cleanup depending on assertion success.
  • 2Starting afterEach Function without a resource created independently by each test makes the result nondeterministic.
  • 3For afterEach Function, executing code without asserting mocks, timers, files, connections, or state restored after each test is incomplete.
  • 4Using afterEach Function to cover one shared suite-level resource creates the wrong test boundary.
Best Practices
  • 1Prepare a resource created independently by each test before running afterEach Function.
  • 2Implement afterEach Function with `afterEach()`.
  • 3Make the central afterEach Function assertion prove mocks, timers, files, connections, or state restored after each test.
  • 4Preserve cleanup errors attached to the responsible test whenever afterEach Function fails.
💡Core behavior
  • 1afterEach Function target: cleanup executed after every test in a scope.
  • 2afterEach Function API: `afterEach()`.
  • 3afterEach Function expected result: mocks, timers, files, connections, or state restored after each test.
  • 4afterEach Function primary risk: cleanup failures being hidden or cleanup depending on assertion success.
💡Implementation steps
  • 1Set up afterEach Function with a resource created independently by each test.
  • 2For afterEach Function, invoke the behavior that produces cleanup executed after every test in a scope.
  • 3In afterEach Function, apply `afterEach()` to the observed result.
  • 4Finish afterEach Function by asserting mocks, timers, files, connections, or state restored after each test.
💡Verification
  • 1Run afterEach Function once with input that should satisfy mocks, timers, files, connections, or state restored after each test.
  • 2Add a negative afterEach Function case that must produce a readable failure.
  • 3Repeat afterEach Function from fresh state to reveal shared-data or ordering dependencies.
  • 4Diagnose afterEach Function through cleanup errors attached to the responsible test.
💡Scope
  • 1afterEach Function covers cleanup executed after every test in a scope.
  • 2afterEach Function does not directly prove one shared suite-level resource.
  • 3Mocks and fixtures used by afterEach Function must continue to match its real dependency contracts.
  • 4For evidence outside the afterEach Function process boundary, prefer `afterAll()` when isolation remains safe.
Summary
  • afterEach Function setup: a resource created independently by each test.
  • afterEach Function action: `afterEach()`.
  • afterEach Function assertion: mocks, timers, files, connections, or state restored after each test.
  • afterEach Function diagnostics: cleanup errors attached to the responsible test.
  • afterEach Function boundary: choose `afterAll()` when isolation remains safe for one shared suite-level resource.
🧑‍💻Interview Questions
Q1. What does afterEach Function verify?
Answer: afterEach Function verifies cleanup executed after every test in a scope.
Q2. Which Jest API is central to afterEach Function?
Answer: The central afterEach Function API is `afterEach()`.
Q3. What proves afterEach Function passed?
Answer: A passing afterEach Function test shows mocks, timers, files, connections, or state restored after each test.
Q4. What makes afterEach Function unreliable?
Answer: A common afterEach Function cause is cleanup failures being hidden or cleanup depending on assertion success.
Q5. When should another test type replace afterEach Function?
Answer: Replace afterEach Function with `afterAll()` when isolation remains safe for one shared suite-level resource.
🎯Quick Quiz

Which approach correctly implements afterEach Function?