question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw error not working

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
meebercommented, Jul 6, 2016

If the thrown error is being caught in a try, then it would not be caught by Chai’s .throw assertion.

0reactions
meebercommented, Jul 6, 2016

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:

try {
  run(...);
} catch (e) {
  someSynchronousLoggingCall();
  throw e;
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found