Bird
Raised Fist0
Node.jsframework~20 mins

Test lifecycle hooks (before, after) in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mocha Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the order of console logs with before and after hooks?
Consider this Node.js test code using Mocha framework lifecycle hooks. What will be the order of console output when running this test suite?
Node.js
describe('Suite', () => {
  before(() => console.log('before all'));
  after(() => console.log('after all'));
  beforeEach(() => console.log('before each'));
  afterEach(() => console.log('after each'));

  it('test 1', () => console.log('test 1'));
  it('test 2', () => console.log('test 2'));
});
Abefore each, before all, test 1, after each, before each, test 2, after each, after all
Bbefore all, before each, test 1, before each, test 2, after each, after each, after all
Cbefore all, before each, test 1, after each, before each, test 2, after each, after all
Dbefore all, test 1, before each, after each, test 2, after each, before each, after all
Attempts:
2 left
💡 Hint
Remember that before runs once before all tests, beforeEach runs before each test, and similarly for after and afterEach.
❓ state_output
intermediate
2:00remaining
What is the final value of counter after hooks and tests?
Given this test code using Mocha hooks, what is the final value of the variable counter after all tests run?
Node.js
let counter = 0;
describe('Counter Suite', () => {
  before(() => { counter += 1; });
  beforeEach(() => { counter += 2; });
  afterEach(() => { counter += 3; });
  after(() => { counter += 4; });

  it('test 1', () => {});
  it('test 2', () => {});
});
A15
B17
C16
D18
Attempts:
2 left
💡 Hint
Count how many times each hook runs and add their increments carefully.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in Mocha hooks?
Identify which code snippet will cause a syntax error when defining Mocha lifecycle hooks.
Abefore(() => { console.log('start'); });
Bafter(function() { console.log('cleanup'); });
CafterEach(() => console.log('done'));
DbeforeEach { console.log('setup'); }
Attempts:
2 left
💡 Hint
Check the syntax for arrow functions and function calls.
🔧 Debug
advanced
2:00remaining
Why does the after hook not run in this test suite?
Given this Mocha test suite, why does the after hook never execute?
Node.js
describe('My Suite', () => {
  before(() => { throw new Error('fail'); });
  after(() => console.log('after hook'));

  it('test 1', () => {});
});
ABecause the after hook is defined after the before hook.
BBecause the before hook throws an error, aborting the suite.
CBecause the test fails, Mocha stops running hooks.
DBecause the test throws an error, the after hook is skipped.
Attempts:
2 left
💡 Hint
Consider how Mocha handles failures in before hooks.
🧠 Conceptual
expert
2:00remaining
Which hook is best to reset a database connection before each test?
In a Node.js test suite using Mocha, you want to reset the database connection before every test to ensure a clean state. Which lifecycle hook should you use?
AbeforeEach()
Bbefore()
Cafter()
DafterEach()
Attempts:
2 left
💡 Hint
Think about how often you want the reset to happen relative to tests.

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

  1. Step 1: Understand the role of before hook

    The before hook runs once before all tests in a test suite to prepare the environment.
  2. Step 2: Differentiate from other hooks

    beforeEach runs before each test, and after runs after all tests, so they do not match the question.
  3. Final Answer:

    To run setup code once before all tests in a suite -> Option A
  4. 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

  1. Step 1: Recall Mocha hook syntax

    Mocha uses functions like beforeEach(() => { ... }) to define hooks.
  2. Step 2: Check each option

    beforeEach(() => { /* setup code */ }); uses correct arrow function syntax. before_each(() => { /* setup code */ }); uses invalid function name. beforeEach {} is incomplete syntax. beforeEach: function() { /* setup code */ } uses incorrect object notation.
  3. Final Answer:

    beforeEach(() => { /* setup code */ }); -> Option B
  4. Quick Check:

    Correct hook syntax uses function call with callback [OK]
Hint: Use camelCase and parentheses for hooks [OK]
Common Mistakes:
  • Using underscores instead of camelCase
  • Omitting parentheses
  • Using object notation instead of function call
3. Consider this Mocha test code snippet:
let count = 0;
before(() => { count += 1; });
after(() => { count += 10; });
it('test 1', () => { count += 100; });
it('test 2', () => { count += 1000; });
What is the final value of count after all tests run?
medium
A. 1110
B. 1112
C. 111
D. 1111

Solution

  1. Step 1: Calculate increments in order

    before runs once: count = 0 + 1 = 1.
    Then test 1: count = 1 + 100 = 101.
    Then test 2: count = 101 + 1000 = 1101.
    Finally after: count = 1101 + 10 = 1111.
  2. Step 2: Confirm no other increments

    No beforeEach or afterEach hooks affect count, so final is 1111.
  3. Final Answer:

    1111 -> Option D
  4. Quick Check:

    Sum increments: 1 + 100 + 1000 + 10 = 1111 [OK]
Hint: Add increments in hook and test order [OK]
Common Mistakes:
  • Adding after hook before tests
  • Forgetting before hook runs once
  • Confusing beforeEach with before
4. Given this Mocha test code:
before(() => { console.log('Setup'); });
after(() => { console.log('Cleanup'); });

it('test 1', () => { console.log('Test 1'); });

it('test 2', () => { console.log('Test 2'); });
What is the correct order of console output?
medium
A. Test 1, Test 2, Setup, Cleanup
B. Test 1, Setup, Test 2, Cleanup
C. Setup, Test 1, Test 2, Cleanup
D. Setup, Test 1, Cleanup, Test 2

Solution

  1. Step 1: Understand hook execution order

    before runs once before all tests, so 'Setup' prints first.
  2. Step 2: Tests run in order, then after hook

    Tests print 'Test 1' then 'Test 2'. After all tests, after runs printing 'Cleanup'.
  3. Final Answer:

    Setup, Test 1, Test 2, Cleanup -> Option C
  4. Quick Check:

    before -> tests -> after = Setup, Test 1, Test 2, Cleanup [OK]
Hint: before runs once first; after runs once last [OK]
Common Mistakes:
  • Assuming after runs after each test
  • Mixing test order
  • Thinking before runs before each test
5. You want to open a database connection once before all tests and close it once after all tests. Which hook pair should you use in Mocha?
hard
A. before to open and after to close
B. beforeEach to open and afterEach to close
C. after to open and before to close
D. beforeEach to open and after to close

Solution

  1. Step 1: Identify when to open connection

    Opening once before all tests means using before hook.
  2. Step 2: Identify when to close connection

    Closing once after all tests means using after hook.
  3. Final Answer:

    before to open and after to close -> Option A
  4. Quick Check:

    Open once before all, close once after all [OK]
Hint: Use before/after for once-only setup/cleanup [OK]
Common Mistakes:
  • Using beforeEach/afterEach causing repeated open/close
  • Reversing before and after roles
  • Mixing hooks for different purposes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard