Chai assert error

Chai lets you assert that some code throws an error. The key is to pass the test subject to expect, not the test result.

Here’s how to assert that the trickyCode function throws an error with the message “That was tricky”.

expect(trickyCode).to.throw('That was tricky')

If trickyCode requires an argument, wrap it in another function.

expect(() => trickyCode({ veryTricky: true })).to.throw('That was very tricky')

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.