Hooks in Mocha

Hooks in Mocha

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.

  1. before: It will run once before all the tests in a suite.
  2. after : It will run once after all the tests in a suite.
  3. beforeEach : It will run before each test in a suite.
  4. afterEach : It will runs after each test in a suite.

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:

Mocha Hooks Example

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:

Before hooks

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:

beforeeach hooks

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:

after hooks outout

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:

afterEach hook 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

要查看或添加评论,请登录

Vishal Kumar的更多文章

社区洞察

其他会员也浏览了