Unit Testing with Jest

Damaris Göbel
3 min readOct 5, 2021

Did you know that Jest is developed by Facebook?

I wasn’t aware what kind of amazing technologies like React and Jest Facebook actually developed.

Jest is a JavaScript testing framework. It allows you to write tests in a very simple and quick way.

Look at my repo: https://github.com/damarisGoebel/unittest-jest

Getting started

If you are working on a node project, just open the folder and write

npm init

to create a package.json file for your project.

Then install jest, by typing

npm i jest 

in your terminal.

Add this to your package.json file:

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

This means everytime you type npm test in your terminal, jest will be executed and run tests.

Writing your first test

Start with a simple function like this in a file called sum.js:

// sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;

Create a new file called sum.test.js and add the following test:

// sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});

In the test file you have to import the sum function you want to be tested by requiring it.

The test block is a single test. If you want to write more tests for this function you start with another test function. The string in quotes is just the name of the test.

The expect is nothing but an assertion. The statement is calling the sum function under test with input 1 and 2 expecting the output to be 3.

Run npm test and see that the test has passed.

First unit test written and tested! Yay!

HTML Page with passed and failed tests

Add the node package jest-html-reporter to your project.

npm i jest-html-reporter

Add this configuration in your package.json file

"jest": {   "reporters": [     "default",    [     "./node_modules/jest-html-reporter",     {     "pageTitle": "Test Report"     }    ]  ]}

Open the test-report.html-file in your browser.

Most used matchers

Matchers are used in test writing like the following:

For Equality, using toBe or not.toBe which are the same as equals and not equals.

test("equality matchers", () => { expect(2*2).toBe(4); expect(4-2).not.toBe(1);})

For Truthiness, matchers are null, falsy, and truthy. Anything which is not true is falsy.

test("truthy operators", () => { var name="Software testing help" var n = null expect(n).toBeNull() expect(name).not.toBeNull// name has a valid value expect(name).toBeTruthy()//fail - as null is non success expect(n).toBeTruthy()// pass - null treated as false or negative expect(n).toBeFalsy()// 0 - treated as false expect(0).toBeFalsy()})

For Number Matchers, matchers like greaterThan, lessThan, greaterThanOrEqual, etc. are used.

test("numeric operators", () => { var num1 = 100; var num2 = -20; var num3 = 0;// greater than expect(num1).toBeGreaterThan(10)// less than or equal expect(num2).toBeLessThanOrEqual(0)// greater than or equal expect(num3).toBeGreaterThanOrEqual(0)})

For String Matchers .toMatch or .not.toMatch are used.

test("string matchers",() => { var string1 = "software testing help - a great resource for   testers"// test for success match expect(string1).toMatch(/test/);// test for failure match expect(string1).not.toMatch(/abc/)})

This page helped me understand the principles:

https://www.softwaretestinghelp.com/jest-testing-tutorial/

The examples are also taking from it.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Damaris Göbel
Damaris Göbel

Written by Damaris Göbel

I like to do crazy things with CSS & JavaScript. My brain occasionally runs out of memory so I need to write down my thoughts.

No responses yet

Write a response