Controllers and Guards — Testing · NestJS

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.

What you will learn

Read the theory for Controllers and Guards

All Testing lessons

All NestJS courses

loading types…

What you'll learn

  • Test a controller as a class: spy on the injected service and assert what the handler passed on and returned
  • Test a guard in isolation with a fake ExecutionContext and real metadata read through the Reflector
  • Know what a unit test of a controller cannot see, and leave routing, pipes and status codes to end-to-end tests