Node.js Unit Testing:

Writing and Running Unit Tests:

Mocha and Chai:

JavaScript has a test framework called Mocha which is highly flexible and feature-rich. Chai pairs well with Mocha as an assertion library for human-readable assertions.

Installation:

                                    
                                    npm install --save-dev mocha chai
                                    
                                

Example Application:

write a simple function to test. Create a file 'math.js':

                                    
// math.js
function add(a, b) {
    return a + b;
}

function subtract(a, b) {
    return a - b;
}

module.exports = { add, subtract };
                                    
                                

Writing Tests:

Create a directory named test and a file math.test.js inside it:

                                    
// test/math.test.js
const { expect } = require('chai');
const { add, subtract } = require('../math');

describe('Math functions', () => {
    describe('add()', () => {
        it('should add two numbers correctly', () => {
            expect(add(2, 3)).to.equal(5);
            expect(add(-1, -1)).to.equal(-2);
        });
    });

    describe('subtract()', () => {
        it('should subtract two numbers correctly', () => {
            expect(subtract(5, 3)).to.equal(2);
            expect(subtract(-1, -1)).to.equal(0);
        });
    });
});
                                    
                                

Running Tests::

Add a test script to your 'package.json':

                                    
"scripts": {
    "test": "mocha"
}
                                    
                                

Run the tests:

                                    
                                    npm test
                                    
                                

Jest:

Jest is a delightful JavaScript testing framework with a focus on simplicity.

Installation:

                                    
                                    npm install --save-dev jest
                                    
                                

Example Application:

You can use the same math.js file from the previous example.

Writing Tests:

Create a directory named __tests__ and a file math.test.js inside it:

                                    
// __tests__/math.test.js
const { add, subtract } = require('../math');

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

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

test('subtracts 5 - 3 to equal 2', () => {
    expect(subtract(5, 3)).toBe(2);
});

test('subtracts -1 - -1 to equal 0', () => {
    expect(subtract(-1, -1)).toBe(0);
});
                                    
                                

Running Tests:

Add a test script to your package.json:

                                    
"scripts": {
    "test": "jest"
}
                                    
                                

Run the tests:

                                    
                                    npm test
                                    
                                

Key Differences Between Mocha and Jest:

Configuration:

  • Mocha: Needs more setup typically used with additional libraries like Chai for assertions or Sinon for mocks.
  • Jest: This comes with batteries included providing built-in assertions, mocks and spies.

Ease of Use:

  • Mocha: It can be widely customized because it is very flexible.
  • Jest: In many use cases are easy to set up with zero configuration required.

Features:

  • Mocha: There are extra libraries needed for extended functionalities.
  • Jest: Has snapshot testing, coverage reports, and a powerful mocking library out-of-the-box.