Table of contents
1.
Introduction
2.
Arrow Function
3.
Hook Function
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Arrow and Hook Functions

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Mocha is one of the most important frameworks of Javascript for Node development. It is generally used for asynchronous testing and is compatible with all of the major libraries of Node, including

  • should.js
  • express.js
  • better-assert

We can install the mocha framework using the npm install command,

npm install -g mocha
You can also try this code with Online Java Compiler
Run Code

Let us look at an example,

arrow.js

var assert = require('assert');
describe('arr', function () {
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present in the array', function () {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
  });
});
You can also try this code with Online Javascript Compiler
Run Code

In the above code, we run a test case using Mocha. If the value is not present in the array, we return -1.

Arrow Function

It is not recommended to use Arrow or Lambda functions in Mocha, as there functions are bound and not able to handle the context of Mocha. For example, the below code will fail to execute,

describe('my arrow', () => {
  it('my test', () => {
    this.timeout(100);
    // if the timeout is set as 100 ms then the test will pass, otherwise the test will fail
    assert.ok(true);
  });
})
You can also try this code with Online Javascript Compiler
Run Code

In the above code, we try and set the timeout of the function as 100 ms. If the timeout is established successfully, the test case passes. Otherwise, the test case fails.

Arrow or Lambda functions work flawlessly for non Mocha context.

Hook Function

Mocha provides multiple hooks in its Behaviour Driven Development style interface. There are four types of hooks in Mocha,

  • before()
  • after()
  • beforeEach()
  • afterEach()

Hooks are used to set pre and post conditions for a test.

var assert = require('assert');
describe('hooks_demo', function() {
  before(function() {
    console.log("Before Hook");
  });


  after(function() {
    console.log("After Hook");
  });


  beforeEach(function() {
    console.log("Before Each Hook");
  });


  afterEach(function() {
    console.log("After Each Hook");
  });


  describe('#indexOf()', function () {
    it('should return -1 when the value is not present in the array', function () {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
    it('should return 0 when the value is present in the array', function () {
      assert.equal([1, 2, 3].indexOf(1), 0);
    });
  });
});
You can also try this code with Online Javascript Compiler
Run Code

In the above code, we are executing two tests, and we added all the hooks to demonstrate the functionality of each hook.

We can even invoke multiple hooks of the same type in the code. The hooks can be differentiated based on the description or the function name. For instance,

beforeEach(function() {
  // beforeEach hook
});


beforeEach(function otherBeforeEach() {
  // beforeEach names otherBeforeEach
});


beforeEach('this is the third hook', function() {
  // beforeEach with the description as this is the third hook
});
You can also try this code with Online Javascript Compiler
Run Code

In the above code snippet, we created three beforeEach hooks and differentiated them based on the function name and the description.

FAQs

  1. What is Mocha?
    Mocha is one of the most important frameworks of Javascript for Node development. It is generally used for asynchronous testing and is compatible with all of the major libraries of Node.
     
  2. What are Hook functions used for?
    Hooks are most commonly used to set preconditions and postconditions for a test. They are also used to clean up after the tests are executed.
     
  3. How many types of hooks are there?
    There are four types of hooks in Mocha, namely
    before()
    after()
    beforeEach()
    afterEach()
     
  4. Can we implement multiple beforeEach() hooks in a describe() block?
    Yes, we can implement multiple hooks of the same type in a describe() block. We can distinguish them on the basis of the description or the function name.
     
  5. Are hook synchronous or Asynchronous?
    All the hooks in Mocha can be both synchronous and asynchronous. They behave a lot like regular test cases in any programming language.

Key Takeaways

This Blog covered all the necessary points about the Arrow and Hook functions in Mocha. We further discussed the basics of Mocha. Finally, we mentioned the ways of implementation for hook function.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass