Recall & Review
beginner
What is the main goal of unit testing a service in NestJS?
To test the service's logic in isolation, ensuring each function works correctly without depending on other parts like databases or external APIs.
Click to reveal answer
beginner
How do you create a testing module for a service in NestJS?
Use Test.createTestingModule({ providers: [YourService] }).compile() to set up a module that provides the service for testing.
Click to reveal answer
intermediate
Why do we use mocks or spies in unit testing NestJS services?
Mocks replace real dependencies to isolate the service logic, while spies let us check if certain methods were called, helping us test behavior without side effects.Click to reveal answer
intermediate
What Jest function is commonly used to create a spy on a method in NestJS service tests?
jest.spyOn(object, 'methodName') is used to spy on a method to track calls and control its behavior during tests.
Click to reveal answer
intermediate
How can you test asynchronous methods in a NestJS service?
Use async/await in your test functions and Jest's expect().resolves or expect().rejects to handle promises and verify async behavior.
Click to reveal answer
Which NestJS testing utility helps create a module for unit testing a service?
✗ Incorrect
Test.createTestingModule() sets up a testing module with providers for isolated unit tests.
What is the purpose of mocking dependencies in NestJS service tests?
✗ Incorrect
Mocking isolates the service logic by replacing real dependencies with fake ones.
Which Jest function lets you check if a method was called during a test?
✗ Incorrect
jest.spyOn() creates a spy to track calls to a method.
How do you handle testing an async method in a NestJS service?
✗ Incorrect
Async/await with Jest's resolves/rejects helps test promises properly.
What does the compile() method do in Test.createTestingModule()?
✗ Incorrect
compile() finalizes the testing module setup and prepares it for use.
Explain how to set up and write a basic unit test for a NestJS service method.
Think about creating a testing module, getting the service, and writing a test function.
You got /5 concepts.
Describe why and how you would mock a dependency in a NestJS service unit test.
Focus on isolation and replacing real parts with fake ones.
You got /5 concepts.