Getting Started with JEST

Getting Started with JEST

Hello everyone ! This article will help you get started with unit testing in javascript

Unit Testing

A level of the software testing process where individual units/components of a software/system are tested. The purpose is to validate that each unit of the software performs as designed.

Let's Implement !

Prerequistes

Jest Installation

To create package.json

npm init -y

To install jest

npm install --save-dev jest

Create file named sum.js

Paste below code in sum.js

const sum = (a, b) => a + b;

module.exports = sum;

To test sum function, you have to create another file named sum.test.js !

Paste below code in sum.test.js

const sum = require("./sum");


test("Adds two number", () => {
    expect(
        sum(2, 3)
    ).toBe(5);
})

How to execute tests ?

Change test script in package.json to jest


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

Run

npm test

Code - github.com/addy123d/jeststarter

Thanks for reading ! ✨