Test lifecycle hooks (before, after) in Node.js
📖 Scenario: You are writing tests for a simple calculator module in Node.js. You want to prepare some setup before tests run and clean up after all tests finish.
🎯 Goal: Build a test file using before and after hooks to initialize and reset a variable around your tests.
📋 What You'll Learn
Create a variable
counter initialized to 0Use a
before hook to set counter to 5 before testsWrite a test that checks
counter equals 5Use an
after hook to reset counter to 0 after tests💡 Why This Matters
🌍 Real World
Test lifecycle hooks help prepare and clean up resources like database connections or test data before and after tests run.
💼 Career
Understanding test hooks is essential for writing reliable automated tests in Node.js projects, a common skill in software development jobs.
Progress0 / 4 steps