A. The controller is not created using Test.createTestingModule
B. The test does not mock the service dependency
C. The test is missing async keyword
D. The expect statement uses toBe instead of toEqual
Solution
Step 1: Check controller instantiation
Creating controller directly is allowed but only if it has no dependencies.
Step 2: Identify missing mocking
If the controller depends on a service, it must be mocked to isolate controller logic.
Final Answer:
The test does not mock the service dependency -> Option B
Quick Check:
Missing mocks for dependencies = The test does not mock the service dependency [OK]
Hint: Always mock services when testing controllers with dependencies [OK]
Common Mistakes:
Assuming direct instantiation is always wrong
Forgetting to mock injected services
Confusing toBe and toEqual for strings
5. You want to unit test a NestJS controller method that calls a service method returning a promise. Which approach correctly mocks the service to test the controller method synchronously?
hard
A. Mock the service method with jest.fn().mockResolvedValue(value) and await the controller method in the test
B. Mock the service method with jest.fn().mockReturnValue(value) without async/await
C. Call the real service method without mocking to test actual behavior
D. Mock the service method with jest.fn().mockImplementation(() => { throw new Error(); })
Solution
Step 1: Understand async service method
The service method returns a promise, so the mock must simulate a resolved promise.
Step 2: Use jest.fn().mockResolvedValue
This creates a mock that returns a resolved promise with the given value, allowing the controller method to await it.
Step 3: Await controller method in test
The test must await the controller method to handle the async call properly.
Final Answer:
Mock the service method with jest.fn().mockResolvedValue(value) and await the controller method in the test -> Option A
Quick Check:
Use mockResolvedValue and await async calls = Mock the service method with jest.fn().mockResolvedValue(value) and await the controller method in the test [OK]
Hint: Use mockResolvedValue for async mocks and await calls [OK]
Common Mistakes:
Using mockReturnValue for async methods
Not awaiting async controller methods
Testing real service instead of mocking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.