Mistake: Async in beforeEach
Question
What is wrong with this setup?
Code
beforeEach(() => {
setupDatabase();
});
test('uses db', () => {
expect(db.isReady).toBe(true);
});
Hint
If setupDatabase is async, the test may run before setup completes.
Explanation
If setupDatabase() is asynchronous (returns a Promise), the test runs before the setup completes, so db.isReady may still be false. The fix: make the beforeEach callback async and await the setup: beforeEach(async () => { await setupDatabase(); });. Jest will wait for the Promise to resolve before running tests.
Related Resources
Related Lessons
- Setup and Teardown
Test hooks for setup and cleanup.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.