Unit Testing

Unit Testing

What

Unit testing is a type of testing that verifies the individual units of source code, such as functions, methods, or classes, work correctly. A unit is the smallest part of the software that can be tested.
 

Why

Unit testing is essential as it helps to ensure the correctness of individual functions or methods, regardless of the larger application context. It helps catch bugs early in the development process, making the codebase more robust and maintainable.
 

How

Unit testing can be performed using various tools like Jest, which provide frameworks to write and execute test cases.
 
 
notion image

Jest


 
const sum = require(‘./sum’); test(‘adds 1 + 2 to equal 3’, () => { expect(sum(1, 2)).toBe(3); }); }
 
What
Jest is a popular JavaScript testing framework developed by Facebook. It provides a full-featured testing environment with a smooth setup experience.
 
Why
Jest is used due to its simplicity, strong community support, and comprehensive feature set. It requires minimal configuration, supports a variety of testing strategies (like snapshot testing and mock functions), and provides a robust set of assertion methods.
 
How
To use Jest, it should be installed in your project. Test cases are written in .test.js files using the Jest global object. These test cases can be run from the command line using the Jest command.
 
Alternatives
 
  • Chai
  • Mocha