DEV Community

Funmilayo E. Olaiya
Funmilayo E. Olaiya

Posted on

Get started with NestJS and create a todo "notes" app: creating e2e tests (Part 2)

Welcome back!


If you read my last post: https://dev.to/funmi5/get-started-with-nestjs-and-create-a-todo-notes-app-4c67

I wrote a comprehensive walkthrough on how to get started with using the NestJs framework, my general overview of it and why it should be considered for future projects.

Now, we are back to the part 2 of the post which is about writing end-to-end tests.

My understanding of end-to-end testing and why it is important is that it helps to test an application's workflow from start to finish, it helps to test the endpoints by behaving the way a real user would.

Why I totally love end-to-end testing is because it helps to prevent silly bugs and of course regression - this works in a way that bugs make a feature stop working after on update/upgrade.

Let's get started:

In the project created last time, check the tests folder and you will see that a test had already been created for the entry GET route upon creation - app.e2e-spec.ts.

  • HTTP requests are simulated using the supertest library.
  • Then we initiate a request to the app that looks like that of a real HTTP request.
  • Please, read more about the INestApplication interface, the inbuilt NestJs TestingModule and the flow of operation from the official docs on automated testing here: - https://docs.nestjs.com/fundamentals/testing

Modify the e2e jest config to:

  • changing the regex from e2e-spec.ts to e2e.ts
{
  "moduleFileExtensions": [
    "js",
    "json",
    "ts"
  ],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create a file called note.e2e.ts in your tests folder.

Add the following code:

import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication, HttpStatus } from "@nestjs/common";
import * as request from "supertest";
import { AppModule } from "../src/app.module";
import { CreateNoteDTO } from "../src/dtos/note.dto";
import * as mongoose from "mongoose";

describe("E2E Tests for NOTE Endpoints", () => {
    let app: INestApplication;

    beforeEach(async () => {
        jest.setTimeout(10000);
        const moduleFixture: TestingModule = await Test.createTestingModule({
            imports: [AppModule],
        }).compile();

        app = moduleFixture.createNestApplication();
        await app.init();
    });

    afterAll(async done => {
        await mongoose.disconnect(done);
    });

    it("should create a note", () => {
        const note: CreateNoteDTO = {
            name: "My Travel Plans for 2020",
            description: "Plans to travel to Kenya",
            tags: "Travel",
        };
        return request(app.getHttpServer())
            .post("/note/add")
            .set("Accept", "application/json")
            .send(note)
            .expect(HttpStatus.CREATED);
    });
    it("should update a note", () => {
        const note: CreateNoteDTO = {
            name: "My Travel Plans for 2020",
            description: "Plans to travel to Kenya",
            tags: "Work",
        };
        return request(app.getHttpServer())
            .patch("/note/update/5ead5c1a43ace404e06a7408")
            .set("Accept", "application/json")
            .send(note)
            .expect(HttpStatus.OK);
    });
    it("should get all notes", () => {
        return request(app.getHttpServer())
            .get("/note/all")
            .set("Accept", "application/json")
            .expect(HttpStatus.OK);
    });
    it("should get a note", () => {
        return request(app.getHttpServer())
            .get("/note/5ead5c1a43ace404e06a7408")
            .set("Accept", "application/json")
            .expect(HttpStatus.OK);
    });
    it("should delete a note", () => {
        return request(app.getHttpServer())
            .delete("/note/delete/5ead5c1a43ace404e06a7408")
            .set("Accept", "application/json")
            .expect(HttpStatus.OK);
    });
});

Enter fullscreen mode Exit fullscreen mode

And there you have it. We have successfully created e2e tests for the endpoints!

For part 1: https://dev.to/funmi5/get-started-with-nestjs-and-create-a-todo-notes-app-4c67
For part 3: https://dev.to/funmi5/get-started-with-nestjs-and-create-a-todo-notes-app-documenting-the-api-endpoints-with-nestjs-swagger-part-3-67

For the code: https://github.com/funmi5/nestjs-notes-todo

Top comments (2)

Collapse
 
wiseintrovert_31 profile image
Wise Introvert • Edited

Awesome article!!
One question: how do i delete all the test data from mongodb after/before each test?

Collapse
 
alereca profile image
alereca

Would you mind explaining me why the jest.setTimeout(1000)?