CodeZettaInterview Hub

NestJS · Testing · Junior · Live Coding

Unit test a service with mocked repo — pattern?

Short Interview Answer

TestingModule with service + mock repo provider token; assert interactions and results.

Detailed Explanation

Don't boot the whole app for unit tests. Keep tests fast and focused.

Example

const module = await Test.createTestingModule({ providers: [UsersService, { provide: getRepositoryToken(User), useValue: mockRepo }] }).compile();

Did you know this answer?