Scenario: You want to write tests for your application built using the gothinkster/node-express-realworld-example-app project. This project is a full-stack real-world example of a Node.js application using Express, Prisma, and TypeScript. In this scenario, we will cover how to write tests for the Express routes and controllers using Jest and Supertest.


First, let's set up the testing environment. The project already has a testing suite set up with Jest as the testing framework. Jest is a popular testing framework for JavaScript and TypeScript projects. It comes with a built-in test runner and assertion library.

  1. Familiarize yourself with the testing files and directories:

    • e2e/: End-to-end tests
    • e2e/src/: Source files for end-to-end tests
    • e2e/src/server/: Server-related tests
    • e2e/src/support/: Helper functions for tests
    • e2e/jest.config.ts: Jest configuration file
    • e2e/project.json: Project configuration file
    • e2e/tsconfig.json: TypeScript configuration file
    • e2e/tsconfig.spec.json: TypeScript configuration file for tests
  2. Write tests for Express routes and controllers:

    • Create a new file in e2e/src/server/ named users.test.ts
    • Import the necessary dependencies, including request, supertest, and the controller file you want to test
    • Write test cases for each route and controller function


// users.test.ts
import request from 'supertest';
import app from '../..'; // Assuming your main file is in the root src folder
import { createUser, getUsers, updateUser, deleteUser } from '../controllers/users.controller';

describe('Users', () => {
  it('should create a new user', async () => {
    const response = await request(app).post('/api/users').send(createUser);

  it('should get all users', async () => {
    const response = await request(app).get('/api/users');
    expect(response.body).toHaveLength(10); // Assuming there are 10 users in the database

  it('should update a user', async () => {
    const newUser = { name: 'John Doe' };
    const response = await request(app)
    expect('John Doe');

  it('should delete a user', async () => {
    const response = await request(app).delete('/api/users/1');
  1. Run the tests:
    • In the terminal, navigate to the project root folder and run nx test e2e
    • Jest will run all the tests in the e2e folder

The above example demonstrates how to write tests for the Express routes and controllers using Jest and Supertest. This is just a starting point, and you can expand the tests to cover more edge cases and scenarios as needed.

Tests to verify the answer:

  1. Create a new user
  2. Get all users
  3. Update a user
  4. Delete a user

These tests should cover the basic functionality of the users controller. You can write similar tests for other controllers in the project.