Hooks in Mocha
Vishal Kumar
Sr. Technical Lead at Aristocrat | SDET | Cypress | K6 | JavaScript | Typescript | Playwright | Rest Assured | TestNG | API Testing | Postman | Software Testing
If you are using JavaScript testing framework then hooks will really helpful concept you should know or have handy insight for this.
Mocha provides hooks that allow you to execute code at specific points in the test lifecycle.
These hooks are particularly useful for setup and teardown tasks, ensuring that your tests run in a clean and predictable environment.
There are 4 types of hook in Mocha that ideally used in Cypress ( Cypress.io ) as well.
Syntax for Hooks:
Combined Syntax for all Hooks:
describe('hooks', function () {
before(function () {
// runs once before the first test in this block
});
after(function () {
// runs once after the last test in this block
});
beforeEach(function () {
// runs before each test in this block
});
afterEach(function () {
// runs after each test in this block
});
// test cases
});
Example:
const assert = require('assert');
describe('Mocha Hooks Example', function () {
let count = 0;
// Runs once before all tests
before(function () {
console.log('This runs before all tests');
cy.log('This runs before all tests');
count = 10;
});
// Runs once after all tests
after(function () {
console.log('This runs after all tests');
cy.log('This runs after all tests')
});
// Runs before each test
beforeEach(function () {
console.log('This runs before each test');
cy.log('This runs before each test')
count++;
});
// Runs after each test
afterEach(function () {
console.log('This runs after each test');
cy.log('This runs after each test')
});
it('Test 1: Count should be 11', function () {
assert.strictEqual(count, 11);
});
it('Test 2: Count should be 12', function () {
assert.strictEqual(count, 12);
});
});
Result Output Screen:
Now let's break down with separate hooks by it syntax and examples:
Before:
Syntax:
describe('Test Suite', () => {
before(() => {
// Code to run before all tests in this suite
console.log('Setup for the test suite');
});
it('Test Case 1', () => {
// Test logic here
});
it('Test Case 2', () => {
// Test logic here
});
});
Example:
describe('Using the before hook', function () {
let data;
before(function () {
console.log('This runs once before all tests');
cy.log("This runs once before all tests");
data = { initialized: true };
});
it('Test 1: Data should be initialized', function () {
assert.strictEqual(data.initialized, true);
});
it('Test 2: Data should remain initialized', function () {
assert.strictEqual(data.initialized, true);
});
});
Output:
beforeEach:
领英推荐
Syntax:
describe('Test Suite', () => {
beforeEach(() => {
// Code to run before each test
});
it('Test Case 1', () => {
// Test logic here
});
it('Test Case 2', () => {
// Test logic here
});
});
Example:
describe('Using the beforeEach hook', function () {
let counter;
beforeEach(function () {
console.log('This runs before each test');
cy.log('This is before each block');
counter = 0; // Reset counter
});
it('Test 1: Counter should start at 0', function () {
counter++;
console.log(counter); // Output: 1
});
it('Test 2: Counter should start at 0 again', function () {
counter += 2;
console.log(counter); // Output: 2
});
});
Output:
after:
Syntax:
describe('Test Suite', () => {
after(() => {
// Code to run before each test
});
it('Test Case 1', () => {
// Test logic here
});
it('Test Case 2', () => {
// Test logic here
});
});
Example:
describe('Using the after hook', function () {
let resource;
before(function () {
resource = 'Allocated';
});
after(function () {
console.log('This runs once after all tests');
cy.log("This is after hooks block");
resource = null; // Clean up
});
it('Test 1: Resource should be allocated', function () {
console.log(resource); // Output: "Allocated"
});
it('Test 2: Resource should still be allocated', function () {
console.log(resource); // Output: "Allocated"
});
});
Output:
afterEach:
Syntax:
describe('Test Suite', () => {
afterEach(function () {
// runs after each test in this block
});
it('Test Case 1', () => {
// Test logic here
});
it('Test Case 2', () => {
// Test logic here
});
});
Example:
describe('Using the afterEach hook', function () {
let testLog = [];
afterEach(function () {
console.log('This runs after each test');
cy.log("This is afterEach hook block");
testLog.push('Test completed');
});
it('Test 1: Add a log entry after this test', function () {
console.log('Running Test 1');
});
it('Test 2: Add another log entry after this test', function () {
console.log('Running Test 2');
});
});
Output:
I hope this will helps If you have any queries then please let me know in the comment. Soon I'll be sharing the content will realtime application automation framework with opensource project.
#automation #mocha #cypress