Reputation: 4440
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
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
Reputation: 3642
You can pass a regular expression as the second argument.
assert.throws(myFunction, /Email is required/);
Upvotes: 6