Understanding Jest: A Beginner’s Guide

Introduction to Jest

What is Jest?

Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works out of the box for most JavaScript projects and is widely used in the React community.

Why Use Jest?

  • Zero Configuration: Jest aims to work out of the box, config free, on most JavaScript projects.

  • Great API: Provides a wide range of APIs to write tests in a more expressive and readable manner.

  • Snapshot Testing: Supports snapshot testing to ensure UI does not change unexpectedly.

  • Built-in Coverage Reports: Generates coverage reports for your code with minimal setup.

Setting Up Jest

Installing Jest

To start using Jest, you need to install it first. If you are using npm, run:

npm install --save-dev jest

Configuring Jest

In most cases, Jest works out of the box with minimal configuration. However, you can customize its behavior by adding a jest.config.js file in your project root.

Writing Your First Test

Create a file named sum.test.js. Inside it, write your first test:

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

Advanced Topics

Mock Functions

Jest provides a way to create mock functions and spy on their behavior.

Asynchronous Testing

Jest can handle asynchronous code effortlessly, allowing for testing of promises, async/await functions, etc.

Snapshot Testing

Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.

Coverage Reporting

Jest can collect code coverage information and report it in a variety of formats.

Conclusion

Jest is a powerful and easy-to-use testing framework that can help you write robust tests for your JavaScript applications.


Commonly Used Matchers in Jest with Examples

Jest provides a variety of “matchers” that let you validate different things in your tests. Here are some of the most commonly used matchers along with examples.

.toBe(value)

This matcher compares with ===. It’s used for exact equality. Example:

 
test('two plus two is four', () => {
 
  expect(2 + 2).toBe(4);
 
});
 

.toEqual(value)

This matcher is used for comparing the value of objects or arrays. It performs a deep equality check.

Example:

 
test('object assignment', () => {
 
  const data = {one: 1};
 
  data['two'] = 2;
 
  expect(data).toEqual({one: 1, two: 2});
 
});
 

.toBeNull()

This matcher checks if a value is null. Example:

 
test('null', () => {
 
  const n = null;
 
  expect(n).toBeNull();
 
  expect(n).toBeDefined();
 
  expect(n).not.toBeUndefined();
 
  expect(n).not.toBeTruthy();
 
  expect(n).toBeFalsy();
 
});
 

.toBeUndefined()

This matcher checks if a value is undefined. Example:

 
test('undefined', () => {
 
  const u = undefined;
 
  expect(u).toBeUndefined();
 
});
 

.toBeDefined()

This matcher is the opposite of .toBeUndefined() and checks if a value is defined. Example:

 
test('defined', () => {
 
  const t = 'hello';
 
  expect(t).toBeDefined();
 
});
 

.toBeTruthy()

This matcher checks if a value is truthy. Example:

 
test('true or false', () => {
 
  expect(true).toBeTruthy();
 
});
 

.toBeFalsy()

This matcher checks if a value is falsy. Example:

 
test('false', () => {
 
  expect(false).toBeFalsy();
 
});
 

.toBeGreaterThan(number)

This matcher checks if a value is greater than a certain number. Example:

 
test('greater than', () => {
 
  const value = 10;
 
  expect(value).toBeGreaterThan(9);
 
});
 

.toBeLessThan(number)

This matcher checks if a value is less than a certain number. Example:

 
test('less than', () => {
 
  const value = 10;
 
  expect(value).toBeLessThan(11);
 
});
 

.toBeCloseTo(number, numDigits)

This matcher is used for comparing floating point numbers. Example:

 
test('floating point numbers', () => {
 
  const value = 0.2 + 0.1;
 
  expect(value).toBeCloseTo(0.3, 5);
 
});
 

Conclusion

These are just a few of the matchers available in Jest. They provide a powerful way to write tests in a readable and expressive manner.