Skip to content

Unit Test

asmaomarr edited this page Oct 29, 2023 · 2 revisions

JEST — Unit testing framework

Jest Unit Test Module

Overview

Jest is a popular JavaScript testing framework that is primarily used for testing JavaScript and TypeScript applications. It is often associated with testing web applications and Node.js applications but can be used for testing any JavaScript code, including libraries and components.

Some of the key concepts in Jest:

Test Suites and Test Cases:

Test Suite: A test suite is a collection of test cases that are related to a specific part of your codebase, like a module, a component, or a function. Test suites are defined using the describe function in Jest.
Test Case : A test case, often referred to as a test, is a specific unit of work you want to test within a test suite. Test cases are defined using the it or test functions.

Matchers:

  • Matchers are used to make assertions about the values you are testing. Jest provides a variety of built-in matchers like toBe, toEqual, toMatch, and toContain, which allow you to compare expected values with actual results.

Mocking:

  • Jest has built-in mocking capabilities that allow you to mock functions, modules, and dependencies. This is useful for isolating the code you're testing and ensuring that external interactions are controlled in your tests.

Code Coverage:

  • Jest can generate code coverage reports, showing which parts of your codebase are covered by tests and which are not. This helps you identify areas that may need more test coverage.

Configuration:

  • Jest is highly configurable. You can set up a jest.config.js file to customize various aspects of your testing environment, such as test environment, test regex patterns, and additional setup files.

Asynchronous Testing:

  • Jest supports testing asynchronous code, including promises, async/await, and timers. You can use async and await in your test cases or utilize done callbacks to handle asynchronous operations.

Basic example:

Here's a comprehensive example that showcases Jest for unit testing. In this example, we'll test a JavaScript function that calculates the factorial of a number:

Screenshot 2023-10-29 at 8 16 33 PM Now a unit test for the factorial function using Jest:
  • The factorial function calculates the factorial of a positive integer. If the input is negative, it throws an error.

  • In the factorial.test.js file, we use Jest to create a test suite (using describe) and two test cases (using it) to cover different scenarios.

  • The first test case checks if the factorial function correctly calculates the factorial of positive integers (0, 1, 5, and 10) and returns the expected results using the expect function.

  • The second test case ensures that the function throws the expected error when given a negative input.
 To run these tests, use the npm test command if Jest is set up in your project. Jest will execute the test suite, and you should see output indicating that all tests have passed.

Usage :

  • Jest Usage
  • Testing a Function
  • Test Scenarios

Jest Usage: The example demonstrates how to write Jest test cases, how to use matchers like toBe, and how to handle errors using toThrow. It also shows how to organize tests using describe to group related test cases.

Testing a Function: The primary purpose of the example is to test a JavaScript function named factorial. The factorial function calculates the factorial of a positive integer. It's a common mathematical operation, and we want to ensure that this function works as expected.

Test Scenarios: It tests the behavior of the factorial function when given positive integers (0, 1, 5, and 10). It expects the function to return the correct factorial values for these inputs.

This examples demonstrate how to testing methods and techniques in Jest using Matchers depending on your testing requirements.

Screenshot 2023-10-29 at 8 20 40 PM