Jest Tutorial – Installation and overview

Jest is a JavaScript testing framework that support almost all libraries like angular, vue, react and also works with node, typescript, babel.

Step1: Installation

npm install --save-dev jest

or

yarn add --dev jest

Step2: Add this in package.json to run test case execution mentioned below.

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

Step3: Create a js(JavaScript) file and export the required function.
filename: student.js

function fullName (first, middle, last) {
  return `${first} ${middle} ${last}`;
}

module.exports = fullName;

Step4: Create another js file in which we will write tests to test the code in the js file created in step3.
filename: student.test.js

const student = require('./student');

test('fullname to equal I am a developer', () => {
  expect(sum('I', 'am a', 'developer')).toBe('I am a developer');
});

Step5: Run the test using yarn test or npm test which will show the result either pass or fail.
Sample Message afer running the test
PASS ./student.test.js
✓ fullname to equal I am a developer (5ms)

Official Website:

https://jestjs.io/