Testing in NestJS

Hold up, so why unit test?

Just a few reasons (of many)...

  • Find bugs early
  • Find regressions before someone else finds them
  • Supports easier implementation refactoring
  • Pseudo documentation in code
  • etc...

Tests must be part of Continuous Integration processes as well as able to run on a local developer's machine

Testing in NestJS

What to test?

  • Database interactions
  • API endpoints (end-to-end)
  • ...
  • ...
  • Basically everything...

80% code coverage is required for this contract

Tactical testing principles

  • Avoid having side effects in your functions! (eg. avoid global state)
  • Tests should NOT rely on testing order (run independently)
  • Tests should be repeatable
  • Fuzzing your inputs

Simple Example

Unit testing in NestJS

By Joe Meilinger

Unit testing in NestJS

  • 453