Begin Unit Tests with Jest
What is Unit Testing?
Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This is usually done by developers.
A Brief Intro to Jest
Jest is a framework designed to test the JavaScript codebase. It allows you to write test cases for projects using Babel, TypeScript, Node, React, Angular, Vue, and more.
In this article, we will use JavaScript (ES6)with NPM for ease and convenience.
Adding Jest to your project
To add the jest module to your project, simply run
npm i --save-dev jest
npm i -g jest
After this, run
jest --init
The tool will ask you a few questions, answer them as per your requirement.
This will create a config file named jest.config.js
Creating a sample function
Of course, we would require something to perform our tests on. Below is a sample function to check whether a number is even or odd.
const evenOdd = (number) => { if (number % 2 == 0) {
return 'even'
}
return 'odd'
}
I have written this in a file called app.js
Creating a test file for this function
Let’s create a new folder called __tests__
and add a new file evenOdd.test.js
in it.
Before writing the test cases, we should see the basic syntax and matchers required for the test cases.
- describe: We have ‘describe’ that describes or collects the test cases for the test suite
- test: indicates a test case
- it: alias to test.
- expect: a function that receives the value to be tested and chains other matchers.
- Other matchers (toBe, toBeTruthy, toHaveProperty, etc.). To see other matchers, use this link.
Writing the first test case
Now, let’s open the evenOdd.test.js
file and begin writing our test cases.
const evenOdd = require("../app")describe('Testing the Even-Odd Function',()=>{
test('The function should exist',()=>{
expect(evenOdd).toBeTruthy()
})
})
Notice the code above; the test case checks whether the function exists or not.
To run this, use jest __tests__/evenOdd.test.js
and notice the output below:
As we can see, the result of the test case is PASS.
We can add a few other test cases as shown in the code below:
const evenOdd = require("../app")describe('Testing the Even-Odd Function',()=>{
test('The function should exist',()=>{
expect(evenOdd).toBeTruthy()
}) test('Should return even on passing an even number',()=>{
expect(evenOdd(8)).toBe('even')
}) test('Should return odd on passing an odd number',()=>{
expect(evenOdd(7)).toBe('odd')
})
})
Now, let’s try a test case that fails.
const evenOdd = require("../app")describe('Testing the Even-Odd Function',()=>{
test('The function should exist',()=>{
expect(evenOdd).toBeTruthy()
})test('Should return even on passing an even number',()=>{
expect(evenOdd(8)).toBe('even')
})test('Should return odd on passing an odd number',()=>{
expect(evenOdd(7)).toBe('odd')
})test('Should return invalid number error if a non-digit input is passed',()=>{
expect(evenOdd('neelesh')).toBe('invalid number')
})})
That was all for this article. This was written for beginners who have not been into unit testing. Jest is a great module and is used a lot.
There is much more to this that includes testing async code, mocking functions and constants that we will see in later articles.