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

oryarn 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.jsfunction 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.jsconst 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/