Test that functions throw errors when expected:
function divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
test('throws on division by zero', () => {
expect(() => divide(1, 0)).toThrow();
expect(() => divide(1, 0)).toThrow('Division by zero');
});
Wrap the call in a function so Jest can catch the error.