Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the testing module in NestJS.
NestJS
import { Test, TestingModule } from '@nestjs/[1]';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@nestjs/test' instead of '@nestjs/testing'.
Misspelling the import path as 'tests'.
✗ Incorrect
The correct import path for NestJS testing utilities is '@nestjs/testing'.
2fill in blank
mediumComplete the code to create a testing module with a database provider.
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
Using a module like DatabaseModule in the providers array (use imports for modules).
Using an unregistered service or connection name.
✗ Incorrect
In NestJS testing, database services are typically provided by including the service class in the providers array.
3fill in blank
hardFix the error in the test setup by completing the database connection string.
NestJS
await module.get(DatabaseProvider).connect([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a connection string for a different database type.
Missing quotes around the connection string.
✗ Incorrect
The test database connection string should match the database type used; PostgreSQL is common for NestJS tests with TypeORM.
4fill in blank
hardFill both blanks to mock the database service and clear data before each test.
NestJS
jest.mock('[1]'); beforeEach(async () => { await databaseService.[2](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mocking the module or provider instead of the service.
Calling a non-existent or wrong method like 'reset' without implementation.
✗ Incorrect
Mock the database service file path with jest.mock and call the 'clear' method on the database service instance before each test to reset state.
5fill in blank
hardFill all three blanks to create a test database connection, run migrations, and close connection after tests.
NestJS
await connection.[1](); await connection.[2](); afterAll(async () => { await connection.[3](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'initialize' instead of 'connect'.
Forgetting to close the connection in afterAll.
✗ Incorrect
First connect to the test database, run migrations to set up the schema, and close the connection after all tests.