0
0
NestJSframework~10 mins

Test database strategies 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 in NestJS.

NestJS
import { Test, TestingModule } from '@nestjs/[1]';
Drag options to blanks, or click blank then click option'
Atests
Btesting
Ctest
Dtest-module
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@nestjs/test' instead of '@nestjs/testing'.
Misspelling the import path as 'tests'.
2fill in blank
medium

Complete 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'
ADbConnection
BDatabaseModule
CDatabaseProvider
DDatabaseService
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.
3fill in blank
hard

Fix 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'
A'mongodb://localhost:27017/testdb'
B'mysql://localhost:3306/testdb'
C'postgresql://localhost:5432/testdb'
D'redis://localhost:6379/testdb'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a connection string for a different database type.
Missing quotes around the connection string.
4fill in blank
hard

Fill 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'
Asrc/database/database.service
Bclear
Csrc/database/database.provider
Dreset
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.
5fill in blank
hard

Fill 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'
Aconnect
BrunMigrations
Cclose
Dinitialize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'initialize' instead of 'connect'.
Forgetting to close the connection in afterAll.