What is the purpose of using a test database in NestJS applications?
A test database isolates test data from production data, ensuring tests run safely without affecting real users or data. It helps verify database interactions in a controlled environment.
Click to reveal answer
intermediate
What is the difference between an in-memory database and a real test database?
An in-memory database stores data temporarily in memory and is fast but may not mimic all real database behaviors. A real test database runs on disk and behaves like production but is slower and needs setup.
Click to reveal answer
beginner
How does the 'beforeEach' hook help in test database strategies?
The 'beforeEach' hook resets or seeds the test database before each test, ensuring tests start with a clean and predictable state for reliable results.
Click to reveal answer
intermediate
Why is it important to clean up the test database after tests?
Cleaning up prevents leftover data from affecting other tests, avoids memory leaks, and keeps the test environment consistent and reliable.
Click to reveal answer
intermediate
What is a common strategy to handle database connections in NestJS tests?
Use a dedicated test module to create and manage database connections, often with a separate test database URL, and close connections after tests to free resources.
Click to reveal answer
Which of the following is a benefit of using an in-memory test database?
AExact replication of production behavior
BData persists after tests finish
CFaster test execution
DRequires complex setup
✗ Incorrect
In-memory databases are fast because they store data in memory, but they may not replicate all production behaviors exactly.
What should you do before each test when using a test database?
ASeed or reset the database
BLeave data as is
CDelete the entire database
DRun tests in parallel without setup
✗ Incorrect
Resetting or seeding the database before each test ensures a clean state and reliable test results.
Why is it important to close database connections after tests in NestJS?
ATo keep connections open for faster tests
BTo free resources and avoid memory leaks
CTo prevent tests from running
DIt is not necessary
✗ Incorrect
Closing connections frees resources and prevents memory leaks, keeping the test environment stable.
Which strategy helps isolate test data from production data?
ANot using a database in tests
BRunning tests on production database
CSharing the same database for all environments
DUsing a separate test database
✗ Incorrect
A separate test database keeps test data isolated and safe from production data.
What is a downside of using a real test database compared to an in-memory one?
ASlower test execution
BData is lost after tests
CCannot run queries
DNo need for setup
✗ Incorrect
Real test databases run on disk and are slower than in-memory databases but provide more accurate behavior.
Explain how you would set up a test database strategy in a NestJS project.
Think about isolation, setup, and cleanup steps.
You got /4 concepts.
Describe the advantages and disadvantages of using an in-memory database for testing in NestJS.
Compare speed and realism.
You got /2 concepts.
Practice
(1/5)
1.
Why do we use a separate test database in NestJS applications?
easy
A. To store user passwords securely
B. To keep real data safe and avoid accidental changes during tests
C. To make the application run faster in production
D. To reduce the size of the main database
Solution
Step 1: Understand the purpose of test databases
Test databases are used to isolate testing data from real data to prevent accidental data loss or corruption.
Step 2: Identify the main reason for using test databases in NestJS
Using a separate test database ensures tests do not affect production data, keeping it safe.
Final Answer:
To keep real data safe and avoid accidental changes during tests -> Option B
Quick Check:
Test database = Protect real data [OK]
Hint: Test DB isolates test data from real data [OK]
Common Mistakes:
Thinking test DB improves production speed
Confusing test DB with security for passwords
Assuming test DB reduces main DB size
2.
Which of the following is the correct way to configure an in-memory SQLite database for testing in a NestJS TypeOrmModule?