But the test fails because the database connection is not ready. What is the likely problem?
medium
A. beforeAll should be async and await db.connect()
B. afterAll should run before the test
C. db.getData() should not be awaited
D. expect(data).toBeDefined() is incorrect syntax
Solution
Step 1: Check async setup in beforeAll
Database connection is asynchronous, so beforeAll must be async and await db.connect() to ensure readiness.
Step 2: Verify other parts
afterAll runs after tests, awaiting db.getData() is correct, and expect syntax is valid.
Final Answer:
beforeAll should be async and await db.connect() -> Option A
Quick Check:
Async setup needs await in beforeAll [OK]
Hint: Async setup needs await in beforeAll hook [OK]
Common Mistakes:
Not making beforeAll async for async setup
Misplacing afterAll before tests
Ignoring await on async calls
5. You want to write an integration test for a Node.js app that creates a user via POST /users and then fetches it via GET /users/:id. Which pattern best ensures your test is reliable and isolated?
hard
A. Use setup hook to create a test user, then test GET /users/:id, and cleanup hook to delete the user
B. Run both POST and GET requests in the same test without cleanup
C. Only test GET /users/:id assuming user exists in database
D. Manually add user in database before tests and never delete
Solution
Step 1: Understand test isolation
Tests should not depend on external data; setup creates needed data, cleanup removes it.
Step 2: Apply setup and cleanup hooks
Creating a user in setup, testing GET, then deleting user in cleanup keeps tests reliable and repeatable.
Final Answer:
Use setup hook to create a test user, then test GET /users/:id, and cleanup hook to delete the user -> Option A
Quick Check:
Setup and cleanup keep tests isolated and reliable [OK]
Hint: Setup and cleanup hooks keep tests clean and repeatable [OK]
Common Mistakes:
Not cleaning up test data causing flaky tests
Assuming data exists without setup
Combining unrelated tests without isolation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.