0
0
NestJSframework~20 mins

Test database strategies in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Test Database Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when using an in-memory database for NestJS tests?
You configure your NestJS tests to use an in-memory database like SQLite in memory mode. What is the main behavior you will observe during test runs?
AData from previous test runs remains in the database, causing tests to share state.
BEach test run starts with a fresh empty database, so no data persists between tests.
CThe database runs on disk and requires manual cleanup after tests.
DThe in-memory database automatically syncs with the production database.
Attempts:
2 left
💡 Hint

Think about what 'in-memory' means for data persistence.

📝 Syntax
intermediate
2:00remaining
Which NestJS testing setup correctly resets the test database before each test?
Given the following NestJS test setup, which option correctly resets the database before each test?
NestJS
beforeEach(async () => {
  // reset database here
});
Aawait connection.synchronize(true);
Bawait connection.clear();
Cawait connection.dropDatabase(); await connection.synchronize();
Dawait connection.close();
Attempts:
2 left
💡 Hint

Resetting means removing all tables and recreating them.

🔧 Debug
advanced
2:00remaining
Why does the NestJS test suite fail when using a shared test database without cleanup?
You run your NestJS tests using a shared test database but do not clean data between tests. Tests start failing with unique constraint errors. What is the cause?
AThe test runner does not support parallel execution.
BThe database connection is closed too early causing errors.
CThe test database schema is not synchronized with entities.
DTests share data causing conflicts because previous test data remains in the database.
Attempts:
2 left
💡 Hint

Think about what happens if data is not cleared between tests.

🧠 Conceptual
advanced
2:00remaining
What is the advantage of using transactions in NestJS test database strategies?
Why might you wrap each NestJS test in a database transaction that is rolled back after the test?
AIt isolates test data changes and automatically reverts them, keeping the database clean.
BIt speeds up tests by caching database queries.
CIt permanently saves test data for debugging after tests finish.
DIt allows tests to share data between each other for faster setup.
Attempts:
2 left
💡 Hint

Think about how transactions can undo changes.

state_output
expert
3:00remaining
What is the final count of users in the test database after running this NestJS test code?
Consider this NestJS test code using TypeORM with an in-memory SQLite database. What is the number of users in the database after the test completes?
NestJS
it('adds users and rolls back', async () => {
  await connection.transaction(async manager => {
    await manager.save(User, { name: 'Alice' });
    await manager.save(User, { name: 'Bob' });
    const countInside = await manager.count(User);
    expect(countInside).toBe(2);
    throw new Error('Rollback');
  }).catch(() => {});
  const countAfter = await connection.manager.count(User);
  expect(countAfter).toBe(0);
});
A0 users, because the transaction was rolled back.
B1 user, because only the first save succeeded.
C2 users, because the users were saved before the error.
DTest fails with an unhandled error.
Attempts:
2 left
💡 Hint

What happens to data inside a transaction if an error is thrown?