0
0
Expressframework~20 mins

Test database setup and teardown in Express - 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 if the test database connection is not closed after tests?

Consider an Express app test suite that opens a database connection before tests but does not close it after. What is the likely outcome?

AThe database will automatically close the connection after tests finish.
BTests will run faster because the connection stays open for reuse.
CTests may hang or timeout because the open connection prevents the process from exiting.
DNo effect; tests complete normally and exit immediately.
Attempts:
2 left
💡 Hint

Think about what happens when a Node.js process has open resources.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly sets up and tears down a test database connection in Express tests?

Choose the code that properly opens a database connection before tests and closes it after all tests run.

Abefore(() => { db.connect(); }); after(() => { db.disconnect(); });
BbeforeAll(async () => { await db.connect(); }); afterAll(async () => { await db.disconnect(); });
CbeforeEach(async () => { await db.connect(); }); afterEach(async () => { await db.disconnect(); });
Dsetup(() => { db.connect(); }); teardown(() => { db.disconnect(); });
Attempts:
2 left
💡 Hint

Look for async functions that run once before and after all tests.

🔧 Debug
advanced
2:00remaining
Why does this test suite fail with 'Timeout - Async callback was not invoked' error?

Review the test setup code below and identify the cause of the timeout error.

Express
beforeAll(() => {
  db.connect();
});

afterAll(() => {
  db.disconnect();
});

test('sample test', () => {
  expect(true).toBe(true);
});
AThe beforeAll and afterAll functions are missing async/await, so the test runner does not wait for connection to finish.
BThe test function is missing async keyword, causing the test to timeout.
CThe db.connect() and db.disconnect() functions are synchronous and block the event loop.
DThe test suite is missing a call to jest.runAllTimers() to complete async operations.
Attempts:
2 left
💡 Hint

Consider how asynchronous code should be handled in test lifecycle hooks.

state_output
advanced
2:00remaining
What is the state of the test database after running this setup and teardown code?

Given the following code, what will be the state of the test database after all tests complete?

Express
beforeAll(async () => {
  await db.connect();
  await db.clear();
});

afterAll(async () => {
  await db.disconnect();
});
AThe database connection remains open and the database still contains old data.
BThe database connection remains open and the database is cleared.
CThe database connection is closed but the database still contains old data.
DThe database connection is closed and the database is empty (cleared).
Attempts:
2 left
💡 Hint

Look at what happens before and after all tests regarding connection and data.

🧠 Conceptual
expert
2:00remaining
Why is it important to isolate test database setup and teardown from production database in Express apps?

Explain the main reason for using separate test database setup and teardown processes instead of running tests against the production database.

ATo prevent tests from accidentally modifying or deleting real production data, ensuring data safety.
BBecause test databases automatically generate fake data, unlike production databases.
CTo improve test speed by using a smaller production database copy.
DBecause production databases do not support connections from test suites.
Attempts:
2 left
💡 Hint

Think about the risks of running tests on live data.