Challenge - 5 Problems
NestJS Unit 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 service unit test?
Consider this simple NestJS service and its unit test. What will the test output be when run?
NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class MathService { double(value: number): number { return value * 2; } } // Test file import { Test, TestingModule } from '@nestjs/testing'; import { MathService } from './math.service'; describe('MathService', () => { let service: MathService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [MathService], }).compile(); service = module.get<MathService>(MathService); }); it('should return double the input', () => { expect(service.double(4)).toBe(8); }); });
Attempts:
2 left
💡 Hint
Think about how the service is provided and used in the test module.
✗ Incorrect
The MathService is provided directly in the testing module. The double method returns the input multiplied by 2, so calling double(4) returns 8, matching the expectation. No mocking or dependencies are needed here.
🔧 Debug
intermediate2:00remaining
Why does this NestJS service unit test fail with a TypeError?
Look at this unit test for a NestJS service. Why does it fail with a TypeError: Cannot read property 'getData' of undefined?
NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class DataService { getData(): string { return 'data'; } } // Test file import { Test, TestingModule } from '@nestjs/testing'; import { DataService } from './data.service'; describe('DataService', () => { let service: DataService; beforeEach(() => { service = new DataService(); }); it('should return data string', () => { expect(service.getData()).toBe('data'); }); it('should call getData method', () => { const spy = jest.spyOn(service, 'getData'); service.getData(); expect(spy).toHaveBeenCalled(); }); });
Attempts:
2 left
💡 Hint
Check the order of spy creation and method call.
✗ Incorrect
The spy is created after the method call, so it does not detect the call. To fix, create the spy before calling the method.
📝 Syntax
advanced2:00remaining
Which option correctly mocks a dependency in a NestJS service unit test?
You have a NestJS service that depends on another service. Which test setup correctly mocks the dependency?
NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class UserService { constructor(private readonly authService: AuthService) {} isUserAuthenticated(userId: string): boolean { return this.authService.validateUser(userId); } } // AuthService interface export class AuthService { validateUser(userId: string): boolean { return true; } } // Test file snippet import { Test, TestingModule } from '@nestjs/testing'; import { UserService } from './user.service'; import { AuthService } from './auth.service';
Attempts:
2 left
💡 Hint
Mocking with useValue allows you to replace methods with your own implementations.
✗ Incorrect
Option B correctly mocks AuthService by providing a useValue object with a validateUser method returning true. This allows controlled testing of UserService without calling the real AuthService.
❓ state_output
advanced2:00remaining
What is the value of 'result' after this NestJS service test runs?
Given this service and test, what will be the value of the variable 'result' after the test runs?
NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class CounterService { private count = 0; increment(): number { return ++this.count; } reset(): void { this.count = 0; } } // Test file import { Test, TestingModule } from '@nestjs/testing'; import { CounterService } from './counter.service'; describe('CounterService', () => { let service: CounterService; let result: number; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [CounterService], }).compile(); service = module.get<CounterService>(CounterService); }); it('increments and resets count', () => { service.increment(); service.increment(); service.reset(); result = service.increment(); }); });
Attempts:
2 left
💡 Hint
Think about how the reset method affects the count before the last increment.
✗ Incorrect
The count is incremented twice (count=2), then reset to 0, then incremented once more, so result is 1.
🧠 Conceptual
expert2:00remaining
Which statement best describes the role of TestingModule in NestJS unit tests?
In NestJS unit testing, what is the main purpose of using the TestingModule?
Attempts:
2 left
💡 Hint
Think about how NestJS manages dependencies during tests.
✗ Incorrect
TestingModule allows you to create a test-specific module that provides and injects only the dependencies needed for the test, enabling isolated unit testing.