Testing with Repositories — Techniques · NestJS

Unit-test a service that talks to the database without a database: provide a mock under the repository's token with getRepositoryToken, script its answers, and prove the service's rules from what it asked the repository for.

What you will learn

Read the theory for Testing with Repositories

All Techniques lessons

All NestJS courses

loading types…

What you'll learn

  • Provide a mock repository under getRepositoryToken(Entity) in a testing module, so the service under test never touches a database
  • Script findOneBy, save, create and delete per test with mockResolvedValue and friends, and assert what the service asked for
  • Decide what a repository test proves (the service's rules) and what it cannot (the query), and when an integration test on sqljs is the right tool