Checking Errors With assert.throws()

Kirsty’s suite also checks a refusal. If you pass an unknown ability, such as "racer", the code must raise FittingError instead of choosing skis. Here’s her Python test:

Language: Python
import pytest
from fitting import FittingError, ski_length

def test_unknown_ability():
    with pytest.raises(
        FittingError, match="unknown ability: racer"
    ):
        ski_length(170, "racer")

JavaScript’s assert.throws() does the same job, and a second argument of FittingError checks the class, as in pytest. The first argument is where the port differs.

Hand assert.throws() a Function

The natural port moves the with block’s line into the first argument. Replace fitting.test.js with this test, and predict what node --test reports:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.