Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

JavaScript Essentials - Unit Testing

Writing unit tests for JavaScript code

Unit testing is a crucial practice in software development that helps ensure code quality and reliability. This tutorial covers the basics of writing unit tests for JavaScript code, using popular testing frameworks like Jest and Mocha.

Key Points:

  • Unit tests verify the functionality of individual units of code.
  • Automated testing frameworks like Jest and Mocha simplify writing and running tests.
  • Writing unit tests helps catch bugs early and improves code maintainability.

Setting Up a Testing Environment

To write unit tests, you need to set up a testing environment. Jest and Mocha are popular choices for JavaScript testing.


// Install Jest
npm install --save-dev jest

// Install Mocha
npm install --save-dev mocha
                

Writing Tests with Jest

Jest is a powerful testing framework developed by Facebook. It provides a rich API for writing tests and assertions.


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

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

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

Run the tests using the Jest command:


// Run Jest tests
npx jest
                

Writing Tests with Mocha

Mocha is a flexible testing framework that works well with assertion libraries like Chai.


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

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

describe('Sum', function() {
    it('should add 1 + 2 to equal 3', function() {
        assert.equal(sum(1, 2), 3);
    });
});
                

Run the tests using the Mocha command:


// Run Mocha tests
npx mocha
                

Test Coverage

Test coverage measures how much of your code is tested. Jest provides built-in support for coverage reports, while Mocha can use third-party tools like Istanbul.


// Run Jest with coverage
npx jest --coverage

// Install Istanbul for Mocha
npm install --save-dev nyc

// Run Mocha with coverage
npx nyc mocha
                

Mocking and Spying

Mocking and spying are techniques used to simulate and inspect the behavior of functions and dependencies. Jest has built-in support for mocking, while Sinon.js is commonly used with Mocha.


// Mocking with Jest
const myFunction = jest.fn();
myFunction();
expect(myFunction).toHaveBeenCalled();

// Mocking with Sinon.js
const sinon = require('sinon');
const myFunction = sinon.spy();
myFunction();
sinon.assert.calledOnce(myFunction);
                

Summary

In this tutorial, you learned about writing unit tests for JavaScript code using Jest and Mocha. Unit testing helps ensure your code works as expected, makes it easier to refactor, and improves overall code quality. Incorporate unit testing into your development workflow to catch bugs early and maintain robust code.