What is the purpose of the before hook in testing?
The before hook runs once before all tests in a suite. It is used to set up things needed for the tests, like initializing data or starting a server.
Click to reveal answer
beginner
How does the after hook help in test suites?
The after hook runs once after all tests in a suite. It is used to clean up resources like closing database connections or stopping servers.
Click to reveal answer
intermediate
What is the difference between before and beforeEach hooks?
before runs once before all tests, while beforeEach runs before each individual test. Use beforeEach to reset state before every test.
Click to reveal answer
intermediate
In Node.js testing frameworks like Mocha, when would you use afterEach?
Use afterEach to run cleanup code after every test, such as resetting mocks or clearing temporary data to keep tests isolated.
Click to reveal answer
beginner
Why are lifecycle hooks important in automated testing?
Lifecycle hooks help organize setup and cleanup tasks, making tests reliable and easier to maintain by avoiding repeated code and ensuring a clean test environment.
Click to reveal answer
Which hook runs once before all tests in a suite?
AafterEach
Bafter
CbeforeEach
Dbefore
✗ Incorrect
The 'before' hook runs once before all tests to set up the environment.
What is the main use of the 'after' hook?
ARun cleanup after all tests
BInitialize test data
CRun before each test
DSkip tests
✗ Incorrect
The 'after' hook is used to clean up resources after all tests have run.
Which hook runs before every single test?
AbeforeEach
Bafter
Cbefore
DafterEach
✗ Incorrect
'beforeEach' runs before each test to prepare a fresh state.
If you want to reset mocks after each test, which hook should you use?
AbeforeEach
Bafter
CafterEach
Dbefore
✗ Incorrect
'afterEach' runs after each test, perfect for cleanup like resetting mocks.
Why avoid putting setup code inside each test instead of using lifecycle hooks?
AIt makes tests slower
BIt leads to repeated code and harder maintenance
CIt disables test reporting
DIt causes tests to fail
✗ Incorrect
Using lifecycle hooks avoids repeating setup code and keeps tests clean and maintainable.
Explain how before and after hooks help manage test setup and cleanup in Node.js testing.
Think about what you do before and after a group of tasks in real life.
You got /4 concepts.
Describe the difference between beforeEach and afterEach hooks and when you would use them.
Consider how you prepare and clean your workspace before and after each small task.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the before hook in Node.js testing frameworks like Mocha?
easy
A. To run setup code once before all tests in a suite
B. To run cleanup code after each test
C. To run code before each individual test
D. To skip a test conditionally
Solution
Step 1: Understand the role of before hook
The before hook runs once before all tests in a test suite to prepare the environment.
Step 2: Differentiate from other hooks
beforeEach runs before each test, and after runs after all tests, so they do not match the question.
Final Answer:
To run setup code once before all tests in a suite -> Option A
Quick Check:
before = setup once before all tests [OK]
Hint: Remember: before = once before all tests [OK]
Common Mistakes:
Confusing before with beforeEach
Thinking before runs after tests
Mixing before with afterEach
2. Which of the following is the correct syntax to define a beforeEach hook in Mocha?
easy
A. before_each(() => { /* setup code */ });
B. beforeEach(() => { /* setup code */ });
C. beforeEach {}
D. beforeEach: function() { /* setup code */ }
Solution
Step 1: Recall Mocha hook syntax
Mocha uses functions like beforeEach(() => { ... }) to define hooks.