Skip to content
Advertisement

How to assert that function throws with a specific error message

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

JavaScript

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.’

enter image description here

I also tried

JavaScript

and

JavaScript

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)…

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement