Challenge - 5 Problems
NestJS 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 testing module setup?
Consider this NestJS test setup code. What will be the value of
service.getHello() after the test module is compiled and the service is retrieved?NestJS
import { Test, TestingModule } from '@nestjs/testing'; import { AppService } from './app.service'; describe('AppService', () => { let service: AppService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [AppService], }).compile(); service = module.get<AppService>(AppService); }); it('should return greeting', () => { expect(service.getHello()).toBe('Hello World!'); }); });
Attempts:
2 left
💡 Hint
Look at how the service is provided and retrieved from the testing module.
✗ Incorrect
The testing module provides AppService, so when we get it, calling getHello() returns the default string 'Hello World!'.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in NestJS testing module setup?
Identify which code snippet will cause a syntax error when setting up a NestJS testing module.
NestJS
import { Test, TestingModule } from '@nestjs/testing'; async function setup() { const module = await Test.createTestingModule({ providers: [ { provide: 'MyService', useClass: MyService, } ] }).compile(); return module; }
Attempts:
2 left
💡 Hint
Check the commas between object properties carefully.
✗ Incorrect
In JavaScript/TypeScript object literals, missing commas between properties cause syntax errors.
🔧 Debug
advanced3:00remaining
Why does this NestJS test fail with 'Nest can't resolve dependencies' error?
Given this testing module setup, why does the test fail with a dependency resolution error?
NestJS
import { Test, TestingModule } from '@nestjs/testing'; import { AppService } from './app.service'; import { AppController } from './app.controller'; describe('AppController', () => { let controller: AppController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [AppController], }).compile(); controller = module.get<AppController>(AppController); }); it('should return greeting', () => { expect(controller.getHello()).toBe('Hello World!'); }); });
Attempts:
2 left
💡 Hint
Check what dependencies AppController needs and if they are provided.
✗ Incorrect
AppController depends on AppService, but AppService is not included in the providers array, so Nest cannot resolve it.
❓ state_output
advanced2:30remaining
What is the value of 'result' after this NestJS test runs?
In this test, what will be the value of the variable
result after calling service.getData()?NestJS
import { Test, TestingModule } from '@nestjs/testing'; import { DataService } from './data.service'; class MockDataService { getData() { return ['mock1', 'mock2']; } } describe('DataService', () => { let service: DataService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ DataService, { provide: DataService, useClass: MockDataService }, ], }).compile(); service = module.get<DataService>(DataService); }); it('should return mocked data', () => { const result = service.getData(); expect(result).toEqual(['mock1', 'mock2']); }); });
Attempts:
2 left
💡 Hint
Check which provider is used when multiple providers for the same token exist.
✗ Incorrect
The last provider for DataService uses MockDataService, so service.getData() returns the mock data array.
🧠 Conceptual
expert2:00remaining
Which option best describes the purpose of NestJS TestingModule's compile() method?
What does the
compile() method do when called on a NestJS TestingModule builder?Attempts:
2 left
💡 Hint
Think about what is needed before you can use the module's providers and controllers in tests.
✗ Incorrect
compile() builds the testing module instance and resolves dependencies so you can retrieve providers/controllers for testing.