Jest is a popular open-source JavaScript testing framework designed by Facebook to streamline the process of writing and running tests for the code. It is a framework that stands out because it doesn't need extra tools to work, unlike some of its competitors. It’s easy to use, thanks to its zero-configuration setup, which means we can start using it right after installation without any hassle. Jest is widely used for unit testing and can be easily adapted for integration tests. Its simplicity and ease of use make it accessible to everyone, allowing us to write our first test immediately after installation.
Here’s a breakdown of its key features:
Minimal configuration: Jest is known for its ease of use. Compared to other frameworks that require extensive setup, Jest boasts an “out-of-the-box” experience, allowing us to start writing tests immediately with minimal configuration.
Clear and concise syntax: Jest offers a familiar and easy-to-understand syntax for writing test cases. This makes it approachable for developers of all levels.
Fast test execution: Jest is known for its exceptional speed. It utilizes techniques like test isolation and caching to ensure tests run rapidly, providing quick feedback during development.
Rich matchers and spies: Jest provides a variety of matchers to check if our code works as expected. For example, we can see if a function returns the right value. Jest also has spies, which let us replace parts of our code with mock versions. This helps us test specific sections without relying on other parts. Together, these tools make our tests more accurate and focused.
Broad framework support: While initially created for React applications, Jest has expanded its reach. It can effectively test various JavaScript projects, including those built with frameworks like React Native, Angular, and Vue.js.
Here’s an overview of setting up Jest and testing codes using it:
These two prerequisites of Jest must be installed in the system:
We can verify this by running node -v
and npm -v
in our terminal.
A new project for testing can be created. This project can be initialized using the following command, which will create a sample package.json
in the directory:
npm init -y
The following command is used to install Jest in the Node.js project:
npm install --save-dev jest
package.json
for Node.js:In our package.json
file, we need to specify the test
script:
"scripts": {"test": "jest"}
Jest allows us to write tests in a simple and effective manner. Tests are organized into test suites and test cases. We can create <filename>.js
(or <filename.ts>
and its respective <filename>.test.js
(or <filename>.spec.ts>
files for testing. Here is a sample hello.js
file:
function add(a, b) {return a + b;}module.exports = add;
The above file creates and exports an add()
function that returns the sum of the two parameters. Now, let’s create a test file named hello.test.ts
for testing functionality present in the hello.js
file.
const add = require('./hello');test('adds 1 + 2 to equal 3', () => {expect(add(1, 2)).toBe(3);});test('adds 5 + 7 to equal 12', () => {expect(add(5, 7)).toBe(12);});
The tests in the hello.test.js
file import the add
function from the file named hello.js
. They verify whether add
correctly computes the sum of two numbers by asserting specific expected outcomes using Jest’s expect
function.
We can run Jest from the command line, and it will look for test files with names ending in .test.js
or .spec.js
by default. We can use the following code to run the tests:
npm test
The above command will look for all test files and run those using Jest. If we want to test a particular file, we can use the following command:
npm test <complete_file_path>/<file>.js
Here is a complete executable implementation of the tests we discussed for hello.js
and hello.test.js
files.
{ "name": "jest", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "jest" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "jest": "^29.7.0" } }
Free Resources