Throw error not working
See original GitHub issueHi,
I am trying to catch the error which my script throws in my tests but chai doesnt seem to accept. Following is my code:
config = null; // I am checking for null in the code and throwing an error
expect(() => script.run(cap, mock, config)).to.throw(/No configuration has been supplied/);
I googled and tried various options but still the same. Any help with be highly appreciated.
Cheers!
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Javascript - throw not working in an error handling situation
The problem occurs due to how try .. catch works. Try catch will first, try to do what is inside the bracket (which...
Read more >JavaScript - throw not working in an error handling situation
In output now we will see that the throw statement is now working fine and the error message gets printed successfully as the...
Read more >throw - JavaScript - MDN Web Docs - Mozilla
The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed) ...
Read more >throwError is not throw error - Medium
Throwing an error is, of course, completely different. It's not even a function; it's a statement that interrupts the flow of control. If...
Read more >Exceptions - Manual - PHP
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If the thrown error is being caught in a
try
, then it would not be caught by Chai’s.throw
assertion.So right now your code prints the error to console and then swallows the error so that the rest of your program continues on unaware that an error even occurred. Is it your intention for the error to be swallowed in this way after being logged?
If not, then you’d want something like this:
In the above case, the Chai
.throw
assertion would work as expected.If it is your intention to swallow the error after logging it, then your best bet would probably be to create a logging module, and then stub out the logging method with something like Sinon , and then assert that the stubbed logging method was called with the error.