How to add jest to nodejs project

How to add jest to nodejs project

In this article, we will learn how to add unit tests for our nodejs project.

I will show you how to use mort popular javascript testing library in your project – Jest.

To start working with Jest, open your existing project or create a new one. You can see how to create a new nodejs project in this article.

Open your root project folder in the terminal and run the command below:

npm install --save-dev jest

After it, needs to update the scripts section in the package.json file – update the test command to this:

"scripts": {
    "test": "./node_modules/.bin/jest -i"
  }

Also, I recommend you add jest configuration to the package.json file:

"jest": {
    "clearMocks": true,
    "roots": [
      "__tests__"
    ],
    "setupFiles": [
      "<rootDir>/jest.setup.js"
    ],
    "verbose": true
  },

clearMocks – Automatically clear mock calls and instances between every test. Equivalent to calling Jest.clearAllMocks() between each test.

roots – A list of paths to directories that Jest should use to search for files in.

setupFiles – The paths to modules that run some code to configure or set up the testing environment before each test.

verbose – Indicates whether each individual test should be reported during the run.

Create jest.setup.js file:

touch jest.setup.js

Let’s create some simple functions and try to write unit tests for them using the jest library.

If you are using your project, you can add these simple math function in the right place of your project. For example, if you started a new one, you could run this command to create folders and a math helper.

mkdir app && cd app && mkdir helpers && cd helpers && touch math.js

Add this code to the math.js file:

function addInteger(int1, int2) {
    if (typeof int1 !== 'number' || typeof int2 !== 'number') {
        throw new Error('Invalid type of arguments, only number is allowed here.')
    }
    return int1 + int2;
}

module.exports = {
    addInteger
}

Ensure that our addInteger function works properly to import the math helper addInteger method to the root index.js file.

const mathHelper = require('./app/helpers/math');

const result = mathHelper.addInteger(3, 4);
console.log(result);

And run:

node index.js

As a result, you will see – 7;

Now we should create folders where will be stored our tests:

mkdir __tests__/app/helpers

Create math.test.js file with this code:

const mathHelper = require('../../../app/helpers/math');

describe('Math helper tests', () => {

    test('addInteger returns expected result', () => {
        const expectedResult = 8;
        const int1 = 6;
        const int2 = 2;

        expect(mathHelper.addInteger(int1, int2)).toEqual(expectedResult);
    });

    test('addInteger throw error for non numbers arguments', () => {
        const int1 = 6;
        const int2 = '2';

        expect(() => {
            mathHelper.addInteger(int1, int2);
        }).toThrow(Error('Invalid type of arguments, only number is allowed here.'));
    })
});

To run tests: open root project and run command:

npm run test

The code from this article you can find here.

Leave a Reply

Your email address will not be published. Required fields are marked *