0
0
Node.jsframework~20 mins

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

Choose your learning style9 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.