JavaScript - Unit Testing



Unit testing is must in software development life-cycle. It is a process in which we can test small units of code individually to ensure that they are working correctly. In JavaScript, we can use various unit testing frameworks to test our code. Unit testing help us to find bugs early in the development process. Instead of testing the whole application, we can test small units of code.

Why Unit Testing?

  • Catch bug early : testing smaller parts of code, it becomes easy to catch bug early in the development process.
  • Easier Refactoring: With the help of unit testing, you get confidence to make changes in code, because we know the existing code you have tested is already working.
  • Improves Code Quality: Writing unit tests help us to write better code. It helps us to write modular code which is easy to test.

Unit Testing Frameworks

There are many unit testing frameworks are available in JavaScript. Some of them are most popular and widely used. Some of them are −

  • Jest: Jest is a JavaScript testing framework developed by Facebook. It is widely used for testing JavaScript code. Jest is fast, easy to use and it absolutely provides everything you need for testing.
  • Mocha: Mocha has many feature, It is a JavaScript test framework that run on Node.js and in the browser, and makes asynchronous testing simple and fun. Mocha tests run serially, that allow it for flexible and accurate reporting, when mapping uncaught exceptions to the correct test cases.
  • Jasmine: Jasmine is a behavior-driven development framework for testing JavaScript code. It does not rely on browsers, DOM, or any JavaScript framework. Jasmine is inspired by other testing frameworks such as ScrewUnit, JSSpec, JSpec, and RSpec.
  • QUnit: QUnit is a powerful, easy-to-use JavaScript unit testing framework. It's used by the jQuery project to test its code and plugins but is capable of testing any generic JavaScript code.

Structure of Unit Testing

In common unit test cases there are a few steps those are −

  • Setup: In this part, we set up the environment for the test. We can create objects, initialize variables, etc.
  • Execute: In this part, we execute the code that we want to test.
  • Assert: In this part, we check the result of the execution. We check if the result is as expected or not.
  • Teardown: In this part, we clean up the environment after the test. We can destroy objects, clear variables, etc.

Example of unit test using Jest

Below is a code snippet of unit test using Jest.

Example

function sum(a, b) {
    return a + b;
}
module.exports = sum;

// test.js
const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
    expect(sum(1, 2)).toBe(3);
});

Output

PASS  ./sum.test.js
   adds 1 + 2 to equal 3 (3ms)

In this example, we have a function sum which takes two arguments and returns the sum of those two arguments. We have written a test case for this function to check if the function is working correctly or not.

Advertisements