Testing with Jest: An Introduction

Testing with Jest: An Introduction

Testing is very important in software development. Testing is a process, to evaluate the functionality of a software application with an intent to find whether the developed software met the specified requirements or not and to identify the defects to ensure that the product is defect-free in order to produce the quality product. I will be informing you on how to write basic tests using Jest.

Jest is an open JavaScript testing library from Facebook. Its slogan is “Delightful JavaScript Testing”. Jest can be used to test any JavaScript library.

Now let’s practice! This testing thing really is not that difficult, but it is quite new. The only way to get comfortable with it is to spend some time doing it.

To test with Jest the first thing you have to do is install it. You can either install it using npm or yarn.

If you want to use npm you will need the latest version of node and npm. Go to the npm official website, click on download node js and npm and follow the instructions.

If you want to use yarn, Go to the yarn official website, pick your operating system and follow the instructions below.

After installing npm or yarn, in your command line run:

npm init or yarn init

This is to create a package.json file which holds various metadata relevant to the project. Based on the project, npm will ask you a few questions and will create a basic configuration file with a short description for each option.

After that run:

npm install --save-dev jest

or

yarn add --dev jest

to install Jest into the project

Go to your package.json file and add the following to the first section of the file

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

Special Note on using ES6 import statements with Jest

The current version of Jest does not recognize the import statement. In order for you to be able to use ES6 modules for this testing with Jest you may do the following:

  1. Install the @babel/preset-env package
npm i -D @babel/preset-env

Create a .babelrc file in the project’s root with the following lines of code:

{ "presets": ["@babel/preset-env"] }

This will allow you to use import statements. Note that in the Jest docs a similar instruction is laid out here.

After installing jest, open up your project folder in your preferred code editor.

Create a file called capitalize.js and write your function in it.

const capitalize = (string) => {
   return string.charAt(0).toUpperCase() + string.slice(1);
 };
module.exports = capitalize;
module.exports = capitalize;

After that create another file called capitalize.test.js which will contain the tests for the function.

const capitalize = require('./capitalize');
 test('returns the first letter capitalized', () => {
   expect(capitalize('john')).toBe('John');
 });

Finally, run

yarn test

or

npm run test

and Jest will print this message:

PASS  capitalize/capitalize.test.js
   returns the first letter capitalized (2ms)
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.086s, estimated 2s
Ran all test suites matching /capitalize.test.js/i.

Congratulations! You just successfully wrote your first test using Jest!

Lastly, remember to always write tests for your software. It is very important.