Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the testing module in a NestJS test file.
NestJS
import { Test, TestingModule } from '[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 not for testing.
✗ Incorrect
The '@nestjs/testing' package provides utilities to create testing modules in NestJS.
2fill in blank
mediumComplete the code to create a testing module for the AppService.
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
Providing the controller instead of the service.
Providing unrelated services.
✗ Incorrect
The testing module should provide the service you want to test, here 'AppService'.
3fill in blank
hardFix the error in the test by completing the code to get the service instance.
NestJS
const service = module.[1](AppService); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'find' which is not a method on TestingModule.
Using 'fetch' or 'retrieve' which do not exist here.
✗ Incorrect
The 'get' method retrieves an instance of the provider from the testing module.
4fill in blank
hardFill both blanks to write a simple test that checks if the service is defined.
NestJS
it('should be defined', () => { expect(service).[1](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toBeNull' which checks for null, not existence.
Using 'toBeFalsy' which checks for false-like values.
✗ Incorrect
The 'toBeDefined' matcher checks that the value is not undefined.
5fill in blank
hardFill all three blanks to write a test that checks the service returns the expected string.
NestJS
it('should return "Hello World!"', () => { expect(service.[1]()).[2]([3]); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toBe' which is stricter and may fail for strings.
Using wrong method names or string values.
✗ Incorrect
The test calls 'getHello', expects the result to equal the string 'Hello World!'.