I am trying to write assertions that check error messages in nodeunit. I want the test to fail if the error message doesn't match what I expect. However, it does not seem like the API exists for this. Here is what I am trying to do:
foo.js
function foo() {
...
throw new MyError('Some complex message');
}
foo.test.js
testFoo(test) {
test.throws(foo, MyError, 'Some complex message');
}
I would like testFoo to fail if the error message is not 'Some complex message', but that's not how it works. It seems like 'Some complex message' is just a message that explains the test failure. It is not involved with the assertion. What is the best way to do this in nodeunit?
The following method of nodeunit API
can accept a function for the [error] parameter. The function take the
actualargument and returnstrue|falseto indicate the success or a failure of the assertion.In this way, if you wish to assert that some method throws an
Errorand that error contains some specific message, you should write a test like this:Example:
Output:
Actually any testing framework that implements functions from node.js assert module, support that. For example: node.js assert or Should.js