E2E testing

While unit tests are crucial for verifying the functionality of individual components in isolation, E2E tests focus on testing the entire application and offer additional benefits. This lesson provides an overview of E2E testing in NestJS, covering key concepts, tools, and practices.

What is E2E testing?

E2E testing involves testing an application in a way that closely resembles how a real user would interact with it. Unlike unit tests focusing on isolated code units, E2E tests validate the entire application stack, including the front-end and back-end integration, in order to verify that the app works as intended.

Here are some benefits of E2E tests:

  • Verify integration of components: E2E tests validate the integration of different components and ensure that the entire system works together. We can use it to uncover issues that might only occur when different components interact in a specific sequence.

  • Validate configurations: E2E tests can be run in an environment resembling production. This helps identify issues related to configurations and environment-specific behaviors.

  • Confidence in releases: Because E2E tests cover the entire application, successful tests provide us with higher confidence in the stability and correctness of releases.

E2E testing in NestJS

E2E testing is integral to NestJS development, providing confidence in the entire application’s functionality. In NestJS, Jest is used as the testing framework for E2E tests, and we can use the supertest package for executing HTTP API requests.

The supertest library

When generating a new NestJS app using CLI, NestJS includes E2E testing capabilities immediately out of the box. In the generated app skeleton, the E2E test is configured using the supertest library to simulate HTTP requests.

The supertest library is a widely used NodeJS library for testing HTTP APIs. Developers often integrate supertest with Jest to create E2E tests that simulate HTTP requests to the NestJS application and assert the responses.

Run the following command to install supertest:

Get hands-on with 1200+ tech skills courses.