0
0
NestJSframework~10 mins

Unit testing services in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the testing module for a NestJS service.

NestJS
import { Test, TestingModule } from '@nestjs/[1]';
Drag options to blanks, or click blank then click option'
Acore
Bcommon
Ctesting
Dplatform-express
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from '@nestjs/common' instead of '@nestjs/testing'.
Using '@nestjs/core' which is for core framework features.
2fill in blank
medium

Complete the code to create a testing module for the service.

NestJS
const module: TestingModule = await Test.createTestingModule({ providers: [[1]] }).compile();
Drag options to blanks, or click blank then click option'
AMyService
BAppController
CConfigService
DHttpModule
Attempts:
3 left
💡 Hint
Common Mistakes
Adding controllers or modules instead of the service in providers.
Forgetting to include the service in the testing module.
3fill in blank
hard

Fix the error in retrieving the service instance from the testing module.

NestJS
const service = module.[1](MyService);
Drag options to blanks, or click blank then click option'
AgetService
Bget
Cfetch
Dretrieve
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'getService' or 'fetch'.
Trying to access the service directly without using 'get'.
4fill in blank
hard

Fill both blanks to mock a dependency service in the testing module.

NestJS
providers: [MyService, { provide: [1], useValue: [2] }]
Drag options to blanks, or click blank then click option'
ADependencyService
BMockDependency
CMyService
DRealDependency
Attempts:
3 left
💡 Hint
Common Mistakes
Using the service under test as the dependency token.
Using the real dependency instead of a mock.
5fill in blank
hard

Fill all three blanks to write a test that checks if the service method returns expected data.

NestJS
expect(await service.[1]()).toEqual([2]); jest.spyOn(service, '[3]');
Drag options to blanks, or click blank then click option'
AgetData
B{ id: 1, name: 'Test' }
DfetchData
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in expect and spyOn.
Expecting wrong data structure or value.