Why won't mocha recognise my error? - javascript

I have the following test case in Mocha..
.toThrowError("invalid")
However despite the below code (the code in the if block executes correctly)
try {
let arr = [];
existing.forEach((exists) => {
arr.push(...this.dependencies[exists]);
});
for (let x in arr){
if(existing.indexOf(arr[x]) == -1){
throw new Error('invalid');
}
}
}
catch(e){
console.log(e);
}
I get the following message. What am I doing wrong here?
Expected the function to throw an error matching:
"Invalid Base Permissions"
But it didn't throw anything.

You've caught the error yourself, so as far as mocha is concerned, it didn't throw an error (the error is handled before it gets to the test). If you don't rely on the error being caught in your code, remove the try-catch and it should (assuming the error is actually thrown) work.

Related

Why does the finally stanza of a try..catch..finally block get run before catch?

try {
throw new Error('yikes!');
} catch (e) {
throw e;
} finally {
console.log('caught error!')
}
Prints out:
caught error!
Error: yikes!
So is the finally block running before the throw statement?
It only looks like the finally block ran first, but we can see this isn't actually the case:
let failed;
try {
throw new Error('yikes!');
failed = false;
} catch (e) {
failed = true;
throw e;
} finally {
console.log(`caught error! failed: ${failed}`)
}
prints
caught error! failed: true
Error: yikes!
So why is the printing of the error out of band? is there some asynchronous behaviour here that I'm not seeing?
Try runs - Your try runs and throws the error
Catch runs - Your catch catches the error and simply re-throws it.
Finally runs - You print out your string
The rethrown error is now uncaught - And your browser logs details about the error
The finally clause runs before the overall try block finishes. The error that's logged happens when the runtime intercepts the exception, which is after the try block finishes. (Note that your catch code does not console.log() anything. It throws a new exception, but the finally clause will still run before the rest of the world sees that.)
All languages with try catch finally that I know of behave in exactly the same way. The point of finally blocks is to provide a place to put code that is guaranteed to run whether the try succeeds or fails.
The throw statement doesn't print anything. The error is printed later when the browser catches the thrown error.

Catch invalid variable name with SyntaxError

First off the catch clause isn't being executed. But I am getting "SyntaxError: missing exponent" error in the browser. I'm wondering if it's because of load time? Or my setup is not right.
I want to make a custom error for incorrect variable name.
try {
var 1ele = 1;
} catch (error) {
if (error instanceOf SyntaxError) {
throw new SyntaxError("There is a syntax error!");
}
}
Thanks for the help.
You have introduced a Syntax error in your code var 1ele = 1; and intend to catch the error to display a custom error message. However this won't work in the way you have written now since the Syntax error will cause the JavaScript parser to stop soon after encountering the Syntax error. The catch part of your code will never be reached.
However, you can implement the same using an eval statement. Try the code below.
try {
eval("var 1ele = 1");
} catch (error) {
if (error instanceof SyntaxError) {
throw new SyntaxError("There is a syntax error!");
}
}
Here, I have wrapped the Syntax error causing variable declaration inside an eval function. This will cause the syntax error only in the environment where the eval statement is executed and not your main code. You will be able to catch the error and display your custom message.
NOTE 1: You are getting the "SyntaxError: missing exponent" error message because your variable name starts with 1e which makes the JavaScript parser think that it is a number in exponential format.
NOTE 2: You have used instanceOf in your code. It is actually instanceof (with a lower case o). Ref

Disable error handling for mocha.js

Does anyone know if there a way to disable error handling by Mocha?
I'd really like to let the exception bubble up and be handled by the browser during debugging.
Update:
The context is this.
- Some code throw's an unexpected error.
- I would like to disable "try/catch" (something like jasmine's tests do) and be thrown into the debugger in chrome.
This way I can inspect the current state of the application at that moment.
It sounds like there isn't something built in, and I will need to manually do this each time I get some odd error in a test. Maybe by wrapping my code with
try {
somethingThatBreaks();
} catch(e) {
debugger;
}
Mocha installs its own onerror handler to trap exceptions that may occur in asynchronous code. You can disable it with:
window.onerror = undefined;
This is dangerous. You should do this only when you must absolutely do so. If you make this a routine thing, Mocha will completely miss errors that happen in asynchronous code.
If what you want to do is let exceptions in synchronous code bubble up, I'm not seeing what this would accomplish. If you want to inspect the exception before Mocha touches it, a try...catch block wrapping your test will do this.
Mocha's error handling is contained in the following code:
if (this.async) {
try {
this.fn.call(ctx, function(err){
if (err instanceof Error || toString.call(err) === "[object Error]") return done(err);
if (null != err) return done(new Error('done() invoked with non-Error: ' + err));
done();
});
} catch (err) {
done(err);
}
return;
}
// sync
try {
if (!this.pending) this.fn.call(ctx);
this.duration = new Date - start;
fn();
} catch (err) {
fn(err);
}
You could possibly throw the error with the catch (or remove the try..catch entirely) however it would most likely break the test suite if a test fails.
mocha.options.allowUncaught=true
But in this case, the rest of the tests will break if the error is not caught within mocha. Disabling try{} catch(){} in mocha will only be useful when debugging.
To create a test, I suggest using the construction
(for browser)
try{
throw new Error()
} catch (e){
// Let's say it's called in a yor error listeners.
e.handler=()=>{
done(e)
}
let event= new ErrorEvent('error',{error:e})
window.dispatchEvent(event);
}

Logging try{ ... } catch(e){ ... } using ajax : possible?

When I try the following in the chrome console, i get 'undefined' as the value of 'e':
try{
var test=somethingInvalid();
}
catch(e){
console.log(e);
}
How do I access the details of e?
What properties does it have?
Thanks.
Doh. Doing console.log('E is: '+e) seams to work. Sorry!
Well, it will return undefined, because the last line console.log doesn't return a value.
try{
false = true;
}
catch(e){
console.log(e);
}
but with the above you will also see an object > Reference Error followed by the next line of undefined
According to MDN, it is the value on the throw statement.
https://developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch
The exception identifier
When an exception is thrown in the try block, exception_var (e.g.
the e in catch (e)) holds the value specified by the throw statement.
You can use this identifier to get information about the exception
that was thrown.
This identifier is local to the catch clause. That is, it is created
when the catch clause is entered, and after the catch clause finishes
executing, the identifier is no longer available.
As far as I can see, Chrome does output the details of 'e'. I just did this:
var err;
try { /* what you did */ }
catch (e) { err = e; }
upon which Chrome shows you the details of 'e'.
In this case, you can perform calls to see the contents of the following:
err.name
err.arguments
err.type
err.stack
Good luck!
although all the answers provided were very helpful, what we were infact looking for was:
try{
var test=somethingThatDoesntExist();
}
catch(e){
console.log(e.message);
}
e.message is the variable we want to log, as it (to the best of our knowledge) contains a string representation of the error that was caught.
thanks!
I have a custom assert() function that pulls a stack trace of functions (in all modern browsers, except IE):
if (!condition) {
try {
throw Error();
} catch (e) {
// Browsers Firefox, Chrome, Opera provide additional stack property;
// Check if it does not exist (IE);
stackTrace = (!e.stack) ? "No stack available" : e.stack;
self.reportError(errorType, stackTrace, message);
}
}
Obviously, there is a lot more going on than what I'm showing, but the idea is there.

JavaScript: Catch not hit on "Uncaught SyntaxError" [Chrome]

Can someone explain to me why an "Uncaught SyntaxError" isn't handled by a try/catch? Is it possible to gracefully handle this type of error so the rest of the JS runs?
For example:
try { response.write(;); }
catch(e) { console.log(e); }
This code throws a "Uncaught SyntaxError: Unexpected token ;" [Chrome] browser error instead of jumping to the catch and logging the error object. You get similar results in Firefox as well; a thrown error instead of handling it with the catch.
A syntax error is not a run-time exception. In order for your program to be able to throw and catch exceptions, it needs to be able to run. In order to run, it needs to be javascript. The above example is not valid javascript. The interpreter gives up and prints an error when it sees the first invalid line and it never even gets to the "catch" line.
Well, since this is a syntax exception, it means that javascript parser failed to parse you code. Since browser failed to parse your code, it can not execute it. It does not even know about your try/catch block. try/catch block can only catch exception thrown as a result of code executing in context of the block.
You could use a workaround for this.
try {
Function('response.write(;);')
} catch(e) {
console.log(e);
}
Could be used for determining es6 template string support this way:
var isEs6Template;
try {
Function('``');
isEs6Template = true;
} catch(e) {
isEs6Template = false;
}
console.log('es6 template is', isEs6Template ? 'supported' : 'unsupported');

Categories