Challenge - 5 Problems
NestJS Controller Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this NestJS controller unit test?
Consider this simple NestJS controller and its unit test. What will the test output be when run?
NestJS
import { Test, TestingModule } from '@nestjs/testing'; import { CatsController } from './cats.controller'; import { CatsService } from './cats.service'; class MockCatsService { findAll() { return ['cat1', 'cat2']; } } describe('CatsController', () => { let catsController: CatsController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [CatsController], providers: [{ provide: CatsService, useClass: MockCatsService }], }).compile(); catsController = module.get<CatsController>(CatsController); }); it('should return an array of cats', () => { expect(catsController.findAll()).toEqual(['cat1', 'cat2']); }); });
Attempts:
2 left
💡 Hint
Look at how the service is mocked and injected into the controller.
✗ Incorrect
The test uses a mock service that returns ['cat1', 'cat2']. The controller calls this service method, so the test expects and receives this array, passing successfully.
📝 Syntax
intermediate2:00remaining
Which option correctly mocks a service method in a NestJS controller test?
You want to mock the 'create' method of a service in your NestJS controller unit test. Which option correctly sets up the mock?
NestJS
const mockService = {
create: jest.fn().mockReturnValue('created'),
};Attempts:
2 left
💡 Hint
Check the Jest syntax for mocking functions.
✗ Incorrect
Option A correctly uses jest.fn() with mockReturnValue to mock the 'create' method. Option A is a simple function but not a Jest mock. Option A uses incorrect syntax. Option A uses 'returns' which is not a Jest method.
🔧 Debug
advanced2:00remaining
Why does this NestJS controller test fail with 'TypeError: Cannot read property'?
Given this test code, why does it throw a TypeError when calling the controller method?
NestJS
describe('UsersController', () => { let usersController: UsersController; let usersService: UsersService; beforeEach(async () => { const module = await Test.createTestingModule({ controllers: [UsersController], providers: [UsersService], }).compile(); usersController = module.get<UsersController>(UsersController); // usersService is not assigned here }); it('should call service method', () => { jest.spyOn(usersService, 'findAll').mockReturnValue(['user1']); expect(usersController.findAll()).toEqual(['user1']); }); });
Attempts:
2 left
💡 Hint
Check if usersService is assigned before using it.
✗ Incorrect
The test never assigns usersService from the module, so it remains undefined. Calling jest.spyOn on undefined causes a TypeError.
❓ state_output
advanced2:00remaining
What is the value of 'result' after this NestJS controller test runs?
In this test, what will be the value of 'result' after calling the controller method?
NestJS
class MockService { getData() { return { count: 5 }; } } let result; describe('DataController', () => { let controller; beforeEach(async () => { const module = await Test.createTestingModule({ controllers: [DataController], providers: [{ provide: DataService, useClass: MockService }], }).compile(); controller = module.get<DataController>(DataController); }); it('should set result to service data', () => { result = controller.getData(); }); });
Attempts:
2 left
💡 Hint
Look at what the mock service returns and what the controller calls.
✗ Incorrect
The mock service's getData method returns { count: 5 }, and the controller calls this method, so result is set to { count: 5 }.
🧠 Conceptual
expert2:00remaining
Which option best explains why mocking dependencies is important in NestJS controller unit tests?
Why do we mock service dependencies when unit testing NestJS controllers?
Attempts:
2 left
💡 Hint
Think about what unit testing means and why isolation matters.
✗ Incorrect
Mocking dependencies isolates the controller's logic so tests focus only on the controller behavior, not on external services or side effects like database calls.