mosquito87
mosquito87

Reputation: 4440

node assert: Test error message

Using node's assert module how can I test the message of an error?

throw new Error('Email is required!');

I'm using assert.throws to check if an error was thrown:

assert.throws(myFunction, Error);

But this does not provide the ability to check the message.

Upvotes: 5

Views: 1729

Answers (2)

Maxim Mazurok
Maxim Mazurok

Reputation: 4138

You can assert a full error object, including the message without using regular expressions:

assert.throws(myFunction, new Error("Email is required"));

This way it also asserts the correct error name (class).

Upvotes: 0

JME
JME

Reputation: 3642

You can pass a regular expression as the second argument.

assert.throws(myFunction, /Email is required/);

Upvotes: 6

Related Questions