Jest JS: What is jestjs? What is it for?
In this article, I will talk about Jest JS, one of the libraries that tests your code, which is our topic for this article. Jest is a library for testing your code before going into production. It is located in the devDependencies section of your package.json file and is only executed during development….
In this article, I will talk about Jest JS, one of the libraries that tests your code, which is our topic for this article. Jest is a library for testing your code before going into production. It is located in the devDependencies section of your package.json file and is only executed during development. Since Blockchain deals with money, we have to test and test and test again, That is why I decided to talk about this library with special affection for this technology, because when I was programming with React, Node JS, and React Native, I procrastinated too much, almost never did tests, and skipped classes. That’s why I decided to talk only about it in this article, but this isn’t the only library for testing your code. I’ll comment on some of them at the end and give you the names, types of tests, and links, but my focus is on Jest because I’m studying Blockchain and using it to test my code, and thus study and read more. That’s why I’m dedicating myself 10000% to my studies to become a good Blockchain Developer.
Visit and read the articles on my blog and help me improve it day by day. Thank you very much.
Donation
Coffee to stay awake, thank you very much for your contribution to keeping our website online.
Jest is an excellent JavaScript testing framework focused on simplicity.
It works with projects that use: Babel, TypeScript, Node, React, Angular, Vue, and much more!
To get started, you have to install it in your project with this command, depending on which installer you are using.
Introduction
Install Jest using your favorite package manager:
npm install --save-dev jest
yarn add --dev jest
pnpm add --save-dev jest
Let’s start by writing a test for a hypothetical function that adds two numbers. First, create a file named sum.js:
function sum(a, b) {
return a + b;
}
module.exports = sum;
Next, create a file called sum.test.js. It will contain our actual test:
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Add the following section to your package.json:
{
"scripts": {
"test": "jest"
}
}
Finally, run the yarn test or npm test, and Jest will display this message:
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)
You have just successfully written your first test using Jest!
This test used expect and toBe to test whether two values were exactly identical. To learn more about other things Jest can test, see Using Matchers.
Well done, friends! All this information is in the Jest doc’s getting-started guide. I just copied it here so you can read it quickly and get excited about studying like me ;-p. I’m already studying, how about you? Come with me and let’s study together. Knowledge doesn’t take up space, and we’ll be less dumb 🙂 For this type of article, it has to be very short, because people like me who always procrastinate are writing this article and it’s a victory.
Using Matchers
Jest uses “matchers” to let you test values in different ways. This document will introduce some commonly used matchers. For the full list, see the expect API doc.
Common Matchers
The simplest way to test a value is with exact equality.
test(‘dois mais dois é quatro’, () => {
expect(2 + 2).toBe(4);
});
In this code, expect(2 + 2) returns an “expectation” object. You typically won’t do much with these expectation objects except call matchers on them. In this code, .toBe(4) is the matcher. When Jest runs, it tracks all the failing matchers so that it can print out nice error messages for you.
toBe uses Object.is to test exact equality. If you want to check the value of an object, use toEqual:
test(‘atribuição de objeto’, () => {
const data = {one: 1};
data[‘two’] = 2;
expect(data).toEqual({one: 1, two: 2});
});
toEqual recursively checks every field of an object or array.
As promised at the beginning of the article, here are nine more libraries and their types of tests. No need to thank me, just sit down and start studying ;-p according to your needs.
Testing Library
- Component and Integration Testing (UI)
- https://testing-library.com
Mocha
- Unit and Integration Tests
- https://mochajs.org
Cypress
- End-to-End (E2E) Tests
- https://www.cypress.io
Playwright
- E2E Testing and Browser Automation
- https://playwright.dev
Puppeteer
- E2E Testing and Chrome/Chromium Automation
- https://pptr.dev
Vitest
- Unit Tests (Native Framework for Vite)
- https://vitest.dev
Jasmine
- Unit and Behavioral Tests (BDD)
- https://jasmine.github.io
Karma
- Tests in Real Browsers
- https://karma-runner.github.io
Selenium
- E2E Testing and Web Automation
- https://www.selenium.dev
Donation
Coffee to stay awake, thank you very much for your contribution to keeping our website online.
Credit image: wallpaperaccess.com
Note
Please help me improve my articles. If you find any serious grammatical errors, I would be very grateful for your help. I am also studying English and use translators and AI to assist me, but there is nothing better than making a new friend and getting help from an expert.
Conclusion
In summary, adopting JEST is essential for a robust testing strategy in JavaScript/TypeScript. Its main advantage is its simplicity of configuration (zero-config for most projects), which speeds up the start of development. JEST offers a complete ecosystem, including a fast test runner, powerful mocking capabilities, built-in code coverage, and a runner for parallelized testing. This combination of ease of use, performance, and built-in features optimizes workflow, ensuring code reliability and a superior development experience (DX), as detailed in its documentation.
Sources







































