Table of contents
1.
Introduction
2.
Testing Synchronous Code
3.
Example
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Synchronous Code

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

Testing Synchronous Code

Synchronous Coding means running one line of code at a time. The code is executed one line at a time in the order that the codes appear.

In Mocha, while Synchronous Coding, we omit callbacks to inform the compiler to continue to the next test case.

Let us look at an example to understand Synchronous Coding.

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);
    });
    it('should return 0 when the value is not present in the array', function () {
      assert.equal([1, 2, 3].indexOf(1), 0);
    });
  });
});

 

In the above code, we run two test cases using Mocha. If the value is not present in the array, we return -1, and in the second test case, if the value is present, we return 0.

Example

Let us look at a complete example, including hooks to understand the concept of Synchronous Coding,

arrow.js

var assert = require('assert');
describe('hooks', 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);
    });
  });
});

 

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

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 is Synchronous Coding?
    Synchronous Coding means running one line of code at a time. The code is executed one line at a time in the order that the codes appear.
     
  3. 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.
     
  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 hooks 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 Synchronous Coding in Mocha. We further discussed the basics of Mocha. Finally, we looked at examples of Synchronous Coding in Mocha.

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