Predict toThrow
Predict Output Medium
Question
Will this test pass or fail?
Code
test('throws check', () => {
expect(() => {
throw new Error('oops');
}).toThrow('oops');
});
Hint
The function does throw an Error with 'oops'.
Explanation
The test passes. toThrow('oops') checks that the provided function throws an error whose message contains 'oops'. Since the function throws new Error('oops'), the assertion matches and the test passes. Note: you must pass a function wrapper, not call the function directly.
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.