#Unit and E2E tests in NestJS

12 messages · Page 1 of 1 (latest)

undone rampart
#

Hello all 👋

I'm writing unit and e2e tests into my NestJS app and I have some questions about it :

1 - Is it worth to write tests for repository files ? My database logic is tested through my module test eventually and the assertions between the two of them are really equivalent.
2 - What is the main difference between testing controller files and module files ?

Thanks a lot for your help, as always 💟

royal fern
#

In my point of view, it only makes sense to test business logic. Anything that merely passes data to the next layer (like a controller) is not worth testing. Repositories might make sense in cases where they contain some logic. Mere reads and writes should have been tested by the library authors and not your application. The general data flow can be covered by e2e tests.

#

The bottom line is - only test what makes sense to you. There's no point of baving hundreds of tests where almost everything is mocked and you're just asserting that what gets in also comes out.

undone rampart
#

Thanks @royal fern for your reply. I ended up testing only my module and service file, because, as you said, controller and repository files are either passing data to the next layer or only database logic, so there is no point to test that. Thanks again for the clarification

proven meteor
#

What does a module test look like for you? 🤔

undone rampart
#

Here is an example of my health.module.e2e-spec.ts

import type { NestFastifyApplication } from "@nestjs/platform-fastify";
import { FastifyAdapter } from "@nestjs/platform-fastify";
import type { HealthCheckResult } from "@nestjs/terminus";
import type { TestingModule } from "@nestjs/testing";
import { Test } from "@nestjs/testing";
import { HealthModule } from "../../../src/health/health.module";
import { E2eTestModule } from "../../../src/test/e2e-test.module";

describe("Health Module", () => {
  let app: NestFastifyApplication;

  beforeAll(async() => {
    const module: TestingModule = await Test.createTestingModule({ imports: [E2eTestModule, HealthModule] }).compile();

    app = module.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
    await app.init();
  });

  afterAll(async() => {
    await app.close();
  });

  describe("GET /health", () => {
    it("should return app health when route is called.", async() => {
      const response = await app.inject({ method: "GET", url: "/health" });
      expect(response.statusCode).toBe(200);
      const expectedHealthCheckResult: HealthCheckResult = {
        status: "ok",
        details: { mongoose: { status: "up" } },
        error: {},
        info: { mongoose: { status: "up" } },
      };
      expect(response.json<HealthCheckResult>()).toStrictEqual(expectedHealthCheckResult);
    });
  });
});
#

Am I doing something wrong ?

proven meteor
#

Okay. It's more akin to an integration or e2e test than a unit test

undone rampart
#

Yes it is a e2e test ! Because it's from a module file

#

Is it relevant ?

proven meteor
#

No, I just thought you were trying to unit test a module and was confused as to how you'd do that 😆

undone rampart
#

Ahah no don't worry ! :p