Learning to use jest for unit test: Creating a npm command line Utility — Part 3

Praveen David Mathew
7 min readFeb 18, 2022

Description:

This is continuation of “How to create a npm commandline utility” we have already setup our utility that takes in user input using inquirer or yargs and displays “Hello User!”

Please have the setup ready, now we will see how to test this using Jest

We will see how to write jest test using typescript and non typescript, and also how to test cjs and typescript project using jest.

The final Implementation is available at : https://github.com/praveendvd/Jest_tutorial

Continure reading to understand how we did it.

Getting started:

lets see how to test CJS project:

Installing jest:

npm install jest --save-dev

Update package.json:

add below property to package.json

"scripts": {
"test": "npx jest --inject-globals false"
},
"jest": {
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 0
}
},
"rootDir": "./",
"verbose": true,
"maxWorkers": 1,
"collectCoverageFrom": [
"src/**/*.js",
"index.js"
]
}

--

--

Praveen David Mathew

An open source advocator/WebdriverIO Projectcommiter/Postman Supernova/Postman-html-extra contributor/Stack overflow sqa moderator/Speaker