Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from '@nestjs/common' instead of '@nestjs/testing'.
Using '@nestjs/core' which is for core framework features.
✗ Incorrect
The NestJS testing utilities are imported from '@nestjs/testing' to create a testing module.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The service to be tested must be listed in the providers array to be instantiated in the testing module.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'getService' or 'fetch'.
Trying to access the service directly without using 'get'.
✗ Incorrect
The correct method to get a provider instance from the testing module is 'get'.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the service under test as the dependency token.
Using the real dependency instead of a mock.
✗ Incorrect
To mock a dependency, provide its token and a mock value in the providers array.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in expect and spyOn.
Expecting wrong data structure or value.
✗ Incorrect
The test calls 'getData' method, expects a specific object, and spies on 'getData' to check calls.