Fullstack CourseLearn by building
Back to week 4

Topic

Nest testing (unit + e2e / Supertest)

Definition

Nest testing uses @nestjs/testing to build isolated module fixtures for unit tests and often Supertest against a bootstrapped Nest app for HTTP e2e contract checks.

In simpler words

You test providers in isolation, and you test routes by sending real HTTP into a test app.

Learn how automated tests assert HTTP behavior without clicking a UI — the backbone of backend confidence.

After this you can

  • Explain TestingModule vs a full NestFactory bootstrap
  • Describe what Supertest asserts (status + body shape)
  • Read a test report and interpret pass/fail lines

How automated tests check a Nest API

Definition

Nest testing uses @nestjs/testing to build isolated module fixtures for unit tests, and Supertest against a bootstrapped app for HTTP e2e checks.

In simpler words

A unit test verifies one provider’s logic; an e2e test sends a real request and asserts the status code and response body.

Official Nest chapter: https://docs.nestjs.com/fundamentals/testing

Unit tests build a TestingModule and mock collaborators so one class is tested in isolation.

E2e tests bootstrap the app and use Supertest to assert both the HTTP status and the JSON shape.

An e2e assertion with Supertest

it('creates a note', () => {
  return request(app.getHttpServer())
    .post('/notes')
    .send({ title: 'Hi' })
    .expect(201)
    .expect((res) => {
      expect(res.body.id).toBeDefined();
    });
});

The test asserts the status code (201) AND the response body shape — both matter for an API contract.

Keep in mind

  • Never make a test pass by weakening the assertion — fix the code instead.
  • Assert the status code AND the response body shape, not just one.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥80% (enforced by the API) to save progress.

Checking your session…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. How does this course teach Nest testing discipline?
Concept
2. Why keep the grader read-only?
Syntax
3. What should HTTP tests assert?
Practical
4. Is compile success enough to skip status asserts?
Logic
5. Why use Nest testing utilities?
Concept
6. What is wrong with production console.log-only verification?
Practical
7. When a milestone check fails, what should you do?
Syntax
8. What do milestone packages encode?
Logic
9. Should learners skip Unauthorized/Forbidden assertions?
Concept
10. Which testing shortcut is wrong?
Practical
11. What does this testing setup enable?
Conceptintermediate
const module = await Test.createTestingModule({
  providers: [
    NotesService,
    { provide: getRepositoryToken(Note), useValue: mockRepo },
  ],
}).compile();
12. What contract does this e2e test protect?
Syntaxadvanced
await request(app.getHttpServer())
  .get('/notes')
  .expect(401);
13. Why override the publisher in tests?
Practicalintermediate
.overrideProvider(NotesEventsPublisher)
.useValue({ publish: jest.fn() })
14. A read-only milestone check like this fails. What should a learner do?
Logicadvanced
// packages/notes-milestones/checks.ts (read-only)
expect(res.status).toBe(403);
15. What does this assertion verify?
Conceptintermediate
expect(res.body).toEqual({
  data: expect.any(Array),
  meta: expect.objectContaining({ total: expect.any(Number) }),
});

Checking your session…