Mistake: Test Depends on Order
Find the Mistake Medium
Question
What is wrong with this test suite?
Code
let user;
test('create user', () => {
user = createUser();
expect(user.id).toBeDefined();
});
test('update user', () => {
user.name = 'Alice'; // depends on previous test
expect(user.name).toBe('Alice');
});
Hint
The second test depends on the first having run.
Explanation
The mistake is test interdependency — the second test depends on user being set by the first test. If tests run in a different order or the first test fails, the second test breaks. The fix: each test should set up its own state. Use beforeEach to create a fresh user for each test, making them independent.
Related Resources
Related Lessons
- Test Best Practices
Writing maintainable tests.
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.