Let’s TEST your JavaScript Code

function sum(a, b) {
return a + b;
module.exports = sum;
if( sum(1,2) == 3 ) {
console.log('Test Pass')
} else {
console.log('Test Fail')

Let’s use JEST.

01. Setup package.json to the project

npm init -y

02. Install Jest as development dependency

npm install --save-dev jest

03. Create the test file


04. Import the function to be tested into the test file

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

05. Write the test description for the test.

test("properly adds two numbers", () => {
// test should come here

06. Write the test using jest matchers

expect(sum(1, 2)).toBe(3);

07. Change the package. json file scripts to run the tests

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"scripts": {
"test": "jest"
"scripts": {
"test": "jest --coverage"



Developer ❤️ JS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store