InteractiveFrameworks

Testing

Tests that run in your browser against real Nest classes: unit tests with the testing module, mocks and spies, controllers and guards in isolation, end-to-end tests over HTTP, and writing the code to make a given test pass. Every test you write is checked by bugs it has to catch.

lessons
6
level
intermediate
time
3 hours
Start with Your First Test

Unit tests

One class at a time: the shape of a test, the testing module that builds a class with its dependencies, mocks that stand in for them, and the enhancers around a handler.

  1. 1
    Your First Test

    Write unit tests for a plain service: the shape of a test, the matchers that say what you expect, and the bugs a test is worth nothing without catching.

    Read the theory
  2. 2
    The Testing Module

    Build a class with its dependencies inside a test with Test.createTestingModule, provide the test's own values under its tokens, and take instances back out with get().

    Read the theory
  3. 3
    Mocks and Spies

    Replace a slow dependency with a mock the test controls, drive the code under test through success and failure, and assert what it asked the dependency for.

    Read the theory
  4. 4
    Controllers and Guards

    Unit-test a controller by spying on its service, and a guard by handing it a hand-made ExecutionContext with real metadata on a fixture class.

    Read the theory

The whole application

Requests through a real application, and the other direction: a test that exists before the code does.

  1. 5
    End-to-end Tests

    Boot the real application inside a test and send it HTTP requests with supertest, asserting status codes, bodies and validation, the things a unit test cannot see.

    Read the theory
  2. 6
    Test First

    Two spec files describe a feature that does not exist yet, a PATCH route and an age filter. Read the failures, write the code, watch them turn green.

    Read the theory