Mocks and Spies — Testing · NestJS

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.

What you will learn

Read the theory for Mocks and Spies

All Testing lessons

All NestJS courses

loading types…

What you'll learn

  • Stand a dependency in with jest.fn() mocks provided under its token, and script their answers per test
  • Test the failure path by making a mock reject, and async code with resolves and rejects
  • Assert interactions with toHaveBeenCalledWith and not.toHaveBeenCalled, and know when a spy on the real class is the better tool