Mistake: toThrow Called Directly
Find the Mistake Medium
Question
What is wrong with this throw test?
Code
test('throws', () => {
expect(throwError()).toThrow('error');
});
Hint
toThrow needs a function wrapper, not a direct call.
Explanation
The mistake is calling throwError() directly instead of wrapping it in a function. If throwError() throws, the error propagates before expect can catch it, causing the test to fail with an unhandled error. The fix: wrap it in a lambda: expect(() => throwError()).toThrow('error');.
Related Resources
Related Lessons
- Jest Basics
Getting started with Jest.
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.