Skip to content
the poor coder

testing jest

Poor Man
Published
Reading time
3 min read
On this page

Testing Jest#

Testing is an important aspect of software development, and Jest is one of the most popular testing frameworks used by developers all over the world. Jest is a JavaScript testing framework developed by Facebook, and it is used to test JavaScript code in a variety of environments such as Node.js, React, Angular, and Vue.

If you are new to Jest, the first step is to install it via npm:

JavaScript

npm install --save-dev jest

Once you have installed Jest, you can start writing tests. Jest comes with a lot of built-in features and tools that make it easy to write and maintain tests. Some of the features of Jest include:

  • Automatically finds and runs tests
  • Provides built-in mocking and stubbing capabilities
  • Generates code coverage reports
  • Supports asynchronous testing
  • Provides a simple and easy-to-use API

Writing Tests with Jest#

Writing tests with Jest is easy. All you need to do is create a new file with a .test.js extension and write your tests inside it. For example:

JavaScript

// math.test.js

const math = require('./math');

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

test('multiplies 2 * 3 to equal 6', () => {
  expect(math.multiply(2, 3)).toBe(6);
});

In the above example, we are testing a simple math module that has two functions: sum and multiply. We are using the Jest test function to define our tests. The expect function is used to make assertions about the code being tested. In this case, we are asserting that the sum of 1 and 2 should be equal to 3, and that the product of 2 and 3 should be equal to 6.

Jest also provides a lot of matchers that can be used to make more complex assertions. For example:

JavaScript

// math.test.js

const math = require('./math');

test('the sum of 1 and 2 is not equal to 4', () => {
  expect(math.sum(1, 2)).not.toBe(4);
});

test('the product of 2 and 3 is greater than 4', () => {
  expect(math.multiply(2, 3)).toBeGreaterThan(4);
});

In the above example, we are using the not.toBe matcher to assert that the sum of 1 and 2 is not equal to 4. We are also using the toBeGreaterThan matcher to assert that the product of 2 and 3 is greater than 4.

Running Tests with Jest#

To run tests with Jest, you simply need to run the jest command in your terminal:

JavaScript

jest

Jest will automatically find all files in your project that end with .test.js and run them. Jest will also display the results of your tests in your terminal:

Text

 PASS  ./math.test.js
  ✓ adds 1 + 2 to equal 3 (2ms)
  ✓ multiplies 2 * 3 to equal 6 (1ms)
  ✓ the sum of 1 and 2 is not equal to 4 (1ms)
  ✓ the product of 2 and 3 is greater than 4 (1ms)

Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total

Jest also provides a lot of configuration options that can be used to customize the behavior of Jest. For example, you can configure Jest to watch your files for changes and rerun tests automatically when changes are made:

JavaScript

jest --watch

Overall, Jest is a powerful and easy-to-use testing framework that can help you write better and more reliable JavaScript code. Whether you are a beginner or an experienced developer, Jest is definitely worth checking out.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC