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?
ATest.createTestingModule()
BModule.forRoot()
CService.register()
DAppModule.create()
✗ 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?
ATo isolate the service logic from external parts
BTo add more features to the service
CTo speed up the app in production
DTo make the service dependent on real databases
✗ 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?
Ajest.mock()
Bjest.fn()
Cjest.spyOn()
Djest.clearAllMocks()
✗ Incorrect
jest.spyOn() creates a spy to track calls to a method.
How do you handle testing an async method in a NestJS service?
AUse setTimeout in tests
BUse callbacks only
CIgnore async behavior
DUse async/await and expect().resolves or expect().rejects
✗ Incorrect
Async/await with Jest's resolves/rejects helps test promises properly.
What does the compile() method do in Test.createTestingModule()?
ARuns the tests
BBuilds the testing module and resolves dependencies
CDeletes the module
DStarts the NestJS server
✗ 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.
Practice
(1/5)
1. What is the main purpose of unit testing a service in NestJS?
easy
A. To verify the service works correctly on its own without dependencies
B. To test the entire application including all modules
C. To check the UI components connected to the service
D. To deploy the service to production automatically
Solution
Step 1: Understand unit testing scope
Unit testing focuses on testing a single piece of code in isolation, here the service alone.
Step 2: Identify what unit testing excludes
It excludes integration with other modules or UI, focusing only on the service logic.
Final Answer:
To verify the service works correctly on its own without dependencies -> Option A
Quick Check:
Unit testing = isolated service check [OK]
Hint: Unit test means testing one piece alone, no dependencies [OK]
Common Mistakes:
Confusing unit test with integration test
Thinking unit test covers UI or deployment
Assuming unit test runs the whole app
2. Which of the following is the correct way to get a service instance in a NestJS unit test?
easy
A. const service = require('MyService');
B. const service = new MyService();
C. const service = module.get<MyService>(MyService);
D. const service = app.getService(MyService);
Solution
Step 1: Use NestJS TestingModule
In unit tests, you create a TestingModule and get the service instance via module.get.
Step 2: Identify correct syntax
The syntax module.get<MyService>(MyService) is the official way to retrieve the service from the test module.
Final Answer:
const service = module.get<MyService>(MyService); -> Option C
Quick Check:
Use module.get to get service instance [OK]
Hint: Always get service from TestingModule with module.get<Type>(Type) [OK]
Common Mistakes:
Instantiating service directly without module
Using app.getService which is not a NestJS method
Using require which is not for NestJS DI
3. Given this test code snippet, what will be the output of service.getData()?
A. You cannot spy on service methods before mocking dependencies
B. The service method findAll should be mocked on the dependency, not service
C. jest.spyOn syntax is incorrect here
D. Missing async keyword on test function
Solution
Step 1: Identify what to mock in unit tests
In unit tests, you mock dependencies of the service, not the service's own methods.
Step 2: Understand why mocking service method is wrong
Mocking service.findAll replaces the method under test, defeating the purpose of testing it.
Final Answer:
The service method findAll should be mocked on the dependency, not service -> Option B
Quick Check:
Mock dependencies, not the service itself [OK]
Hint: Mock dependencies, never the service method you test [OK]
Common Mistakes:
Mocking the service method instead of its dependencies
Confusing spyOn usage with mocking dependencies
Ignoring async test function requirements
5. You want to test a service method that calls two different repository methods internally. How should you set up your unit test to isolate the service logic correctly?
hard
A. Mock both repository methods with expected return values and inject them into the test module
B. Call the real repository methods to test the full flow
C. Mock only one repository method and leave the other real
D. Do not mock anything and test the service directly
Solution
Step 1: Identify dependencies to mock
Since the service calls two repository methods, both should be mocked to isolate service logic.
Step 2: Setup test module with mocks
Provide mock implementations for both methods in the test module providers to control their outputs.
Step 3: Avoid real calls in unit tests
Calling real repository methods would make it an integration test, not a unit test.
Final Answer:
Mock both repository methods with expected return values and inject them into the test module -> Option A
Quick Check:
Mock all dependencies to isolate service logic [OK]
Hint: Mock all called dependencies to isolate service logic [OK]
Common Mistakes:
Mocking only some dependencies causing partial isolation
Calling real methods making it integration test
Not injecting mocks properly into test module
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.