I’m using Node v18 ( with the built-in testrunner) and the package assert/strict
to test that a function call throws an error with a custom error message.
I think my code should look like
assert.throws(() => myFunction(), 'content of error.message goes here');
Unfortunately I get the following error
error: ‘The “error/message” argument is ambiguous. The error message “content of error.message goes here” is identical to the message.’
I also tried
assert.throws( () => myFunction(), error => { assert.ok(error instanceof CustomError); assert.strictEqual(error.message, 'content of error.message goes here'); return true; });
and
assert.throws(myFunction, 'content of error.message goes here');
but unfortunately that didn’t help. This might be a duplicate of node assert: Test error message but I don’t want to pass in regular expressions because there is no need for it.
Does someone know how to fix the assertion?
Advertisement
Answer
From the docs, it looks like you can pass an object (and should for the OP case)…
// tests whether the thrown error has a particular message assert.throws(myFunction, { message: 'content of error.message goes here'});