Jest is a JavaScript-based testing framework that is prominently used to ensure the correctness of the Javascript codebase. It is widely based on production-oriented projects where we need testing things to be done so that no code or snippets get pushed by mistake.
Majorly used with React applications but is versatile enough to handle Javascript projects. Created by Facebook, Jest makes the testing simple by providing features like Zero config setup, snapshot testing, and powerful mocking capabilities. Jest can also be put in the CI/CD pipelines in the GitHub Actions or any other things to make the environment more and more automated to compete with the manual checking error. As it is built on the Node.js platform, Jest is available via the npm package manager.
These are the following topics that we are going to discuss:
Features of Jest
Some of the cool and notable features are as follows:
- Zero Configuration: Jest is easy to set up with no need for entertaining or exclusive configurations.
- Snapshots: Jest works by capturing our components output and in the future comparing the future outputs test runs with the current one to maintain consistency throughout the development.
- Mocking: Jest allows us to mock the functions and modules, making it more simple to isolate components during testing.
- Asynchronous Testing: Jest supports the asynchronous code with async/await, Promises, or callback functions to avoid the NullExceptionError.
- Parallel Testing: Jest uses the power of Parallel running to optimize performance.
- Code Coverage: Jest provides powerful code coverage reports generations to help us by ensuring that our tests cover all the important parts of our codebase simultaneously.
Steps to run the test:
- First install Jest by running:
npm install --save-dev jest
- Add the following line to your existing package.json to enable running the test with the jest framework:
"scripts": {
"test": "jest"
}
- Let us write a basic and simple test to check if a function returns the correct output or not.
- Write this code in your files.
JavaScript
// sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
JavaScript
// sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
- Run the tests using the following command:
npm test
Output:
Test PassedAsynchronous Testing
Testing the asynchronous code with Jest is straightforward. Here lets explore this with an example of promise:
- First install the jest as the dev dependencies.
npm install --save-dev jest
- and then write the following line as the test script in the package.json file
"scripts": {
"test": "jest"
}
Example: This example shows the jest test on the asynchronous function.
JavaScript
// asyncFunction.js
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('data');
}, 1000);
});
};
module.exports = fetchData;
JavaScript
// asyncFunction.test.js
const fetchData = require('./asyncFunction');
test('fetches data correctly', async () => {
const data = await fetchData();
expect(data).toBe('data');
});
asyncFunction.js
- The fetchData function returns a promise that resolves with the string 'data' after a 1-second delay usin setTimeOut.
asyncFunction.test.js
- The test imports the fetchData and uses the Jest framework's test function to check if the fetch is working correctly or not by returning the data. The async keyword allows us to await the promise, ensuring the test waits for the fetchdata results before using except to comapre it with 'data'.
Async Function testing exampleConclusion
Jest is a powerful and a easy-to-use tetsing framework used for the javascript based applications and also for the react based applications specifically. It requires zero configurations setup, extensive feature set including mocking, snapshot testing, and also parallel execution, making it an excellent choice for the developers community more productive. Jest helps individual to make sure that their code behaves a s expected while providing fast feedbacks.
Similar Reads
NodeJS NPM NPM (Node Package Manager) is a package manager for NodeJS modules. It helps developers manage project dependencies, scripts, and third-party libraries. By installing NodeJS on your system, NPM is automatically installed, and ready to use.It is primarily used to manage packages or modulesâthese are
6 min read
npm init NPM (Node Package Manager) is the default package manager for Node and is written entirely in JavaScript. Developed by Isaac Z. Schlueter, it was initially released on January 12, 2010. NPM manages all the packages and modules for Node and consists of command line client npm. NPM gets installed into
3 min read
NPM Bootstrap NPM (Node Package Manager) is the default package manager for the NodeJS JavaScript runtime environment. It allows developers to easily manage project dependencies, share and reuse code, and automate various development tasks. npm is also used for installing, publishing, and managing third-party pac
2 min read
What is NPM? NPM-Node Package Manager is the tool used by most developers within the JavaScript ecosystem. It manages packages and dependencies on behalf of an application in Node.js. It provides a lightweight way of installing, upgrading, configuring, and removing third-party libraries or modules that might be
9 min read
npm run dev When working with Node.js and JavaScript projects, especially those involving frontend frameworks like React, Vue, or Next.js, you often encounter the command npm run dev. This command is pivotal for developers as it initiates the development server, enabling live reloading, hot module replacement,
3 min read