A Quick Start Guide for Jest Installation

Logan McGuire
2 min readOct 18, 2020
Photo by Jon Tyson on Unsplash

Testing has become a very important part of the development process. Within the world of Javascript Jest has become a very popular unit testing framework. With it also being developed by Facebook, Jest works natively with React (and can be added to many Javascript environments). Jest and many other testing frameworks are built to be very easy to read and use, or at least easy to learn and difficult to master. Here I will walk you through how to get Jest up and running quickly with or without a full application.

I am going to start this from an empty repository, so you may be able to skip some steps. The important thing here is to have Node initialized. With that in mind, let’s go. In the command line of your repository:

touch index.js
touch index.test.js
npm init

This will set up the primary file to test and its testing file, then step you through some questions and make the magic happen. Answer them according to your best judgement, except for when prompted:

test command:

You will want to enter:

jest

After that is installed you’ll have index.js, index.test.js, a package.json file, package-lock.json file, and a node_modules folder. At this point we can install Jest. Still in the command line of your repository:

npm install --save-dev jest

At this point we can check our package.json file and should see something like this:

{
"name": "install-practice",
"version": "1.0.0",
"description": "Practicing installation of jest.",
"main": "index.js",
"dependencies": {},
"devDependencies": {
"jest": "^26.5.3"
},
"scripts": {
"test": "jest"
},
"author": "Logan",
"license": "ISC"
}

Alright, those are the basic steps to get started. The next two pieces are not required, but I think they help a lot. If you used VSCode or another editor which supports IntelliSense style completion I recommend installing the Typescript types for Jest. Even if you don’t use Typescript, installing this package makes remembering the arguments, phrasing, and other details much easier and more communicative. To do this, go back to the command line of your repository and enter:

npm install --save @types/jest

Finally some of the commands we’ll be using to run our tests function much better when our file is a Git repository. Make sure to add your node_modules folder to a .git_ignore file. If you are using VSCode it will prompt you to do this after initialization. Once the node_modules is safely added to the .git_ignore file you can proceed to add and commit it.

And there you have it. A repository is fully set up and ready to go for basic Jest testing. Check back next week where I will go through the basics of actually using Jest for a basic unit test. Want to get a head start? Check out their documentation here.

--

--

Logan McGuire

A creator to the core, he enjoys all games (especially collaborative ones), baking bread, and software development.