Questions
10 of 25
1How do you define a TypeORM entity and register it in a NestJS feature module?
2How do you handle TypeORM migrations in a NestJS project?
3How do you run a transaction with Prisma in NestJS?
4How do you perform CRUD operations and add query methods to a Mongoose model in NestJS?
5How do you use Prisma with multiple databases in a single NestJS application?
6What is the difference between find(), findOne(), findOneOrFail(), and the Query Builder in TypeORM?
7What are the two main ways to run a database transaction in TypeORM with NestJS?
8How do you handle Prisma migrations in a CI/CD pipeline?
9How do you add schema middleware (hooks) and instance methods in NestJS Mongoose?
10How do you mock a repository in a NestJS unit test?
11How do you seed a database in a NestJS application?
12How do you implement a Unit of Work pattern to group database operations across repositories in NestJS?
13How do you implement optimistic locking with TypeORM to prevent lost updates in NestJS?
14How do you integrate Prisma into a NestJS application?
15How do you implement soft deletes in NestJS with TypeORM?
16How do you implement multi-tenancy with a shared database in NestJS TypeORM?
17How do you set up TypeORM in a NestJS application?
18How do you implement a custom TypeORM repository in NestJS?
19How do you propagate a TypeORM transaction across multiple services in NestJS?
20What are Prisma middleware and how do you use them for audit logging in NestJS?
21How do you set up Mongoose in a NestJS application and define a schema?
22How do you handle Mongoose transactions for multi-document atomic operations in NestJS?
23What is the repository pattern and why use it in NestJS?
24How do you handle database connection pooling and health checks in NestJS?
25How do you implement database read replicas in NestJS for read/write splitting with TypeORM?
10 / 25

How do you mock a repository in a NestJS unit test?

Difficulty: 5/10
Dependency Injection, Testing, Mocking

Create a mock factory that returns jest.fn() stubs for every Repository method. Provide it using the getRepositoryToken(Entity) token from @nestjs/typeorm — this matches the token used internally by @InjectRepository(). Retrieve the mock after compiling the module with module.get(getRepositoryToken(Entity)).

Repository mock factory and test setup
Repository mocking rules:
  1. 1

    getRepositoryToken(Entity) from @nestjs/typeorm is the correct provider token — matches @InjectRepository().

  2. 2

    Create a reusable mock factory so every test file gets a fresh mock object.

  3. 3

    Mock createQueryBuilder() with a chainable object that returns this for builder methods.

  4. 4

    Use mockResolvedValue() for async repository methods; mockReturnValue() for synchronous ones.

  5. 5

    Retrieve the mock with module.get(getRepositoryToken(User)) to configure per-test return values.

Scenario Questions

0-2 years experience

  1. 1You have a simple UsersService that injects a UsersRepository. How would you write a unit test that verifies the service calls findOne without hitting the real database?
  2. 2If you forget to provide a mock for the repository in a NestJS test module, what error do you expect and how would you fix it?

2-5 years experience

  1. 1While adding pagination to a ProductsService, you notice the unit test fails after refactoring the repository method signature. How would you adjust your mock to keep the test passing?
  2. 2Explain why using jest.fn().mockResolvedValue versus mockImplementation matters when mocking async repository methods in a NestJS test.

5-8 years experience

  1. 1In a large monorepo, multiple services share a common TypeORM repository. How would you structure your mocks to avoid duplication and keep tests fast?
  2. 2Your CI pipeline shows flaky tests when mocking repositories that use transactions. What strategies would you employ to make the mocks reliable at scale?

8+ years experience

  1. 1Your organization is moving from unit tests with hand‑crafted repository mocks to a contract‑testing approach across teams. How would you design a shared mocking library for NestJS repositories that balances flexibility and maintainability?
  2. 2When introducing a new microservice that depends on several external data stores, how would you decide which repositories to mock versus using an in‑memory database for integration testing, considering long‑term maintenance?

Follow-up Questions

  • Why might you choose mockImplementation over mockResolvedValue in this context?
  • How do you ensure your mock stays in sync with repository method signatures?
  • What are the pros and cons of using an in‑memory database versus a pure mock for repository tests?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.