What is Jest?

Jest is a code testing framework, used to make testing simple & fast.

Why Jest?

The assert module does work properly, and will give you the test results that you need. The problem is that the more files & code you need to handle with assert, the harder it gets to separate those tests, run all of the test files together and make it quick! That's why Jest is here to save the day.

How Jest?

// npm is required

/* Create Project Directory (if not already created) */
mkdir myProject
cd myProject

/* Init npm */
npm init -y

/* Install Jest */
// Inside the package.json file, change the key "test": "jest",
npm install --save-dev jest

There you go! Jest is now installed!

Creating Tests

To create a test is quite simple really, just make sure that the file has .test.js in it so Jest knows that this file must be tested, watch:

// sum**.test.js**

/* Syntax */
test([testDescription], () => expect([codeResult]).toEqual([expectedResult]));

/* Example */
test('Simple Sum', () => expect(2 + 4).toEqual(6));

Simply running npm test will return:

Captura de tela de 2021-09-21 15-25-28.png

Separated Test From Code

// sum.js
const sum = (a, b) => a + b;

module.exports = sum;
// sum.test.js
const sum = require('./sum');

test('Simple Sum', () => expect(sum(2, 4)).toEqual(6));

Learning Jest

toBe() vs toEqual()

Common Properties