How to write a test which expects an Error to be thrown in Jasmine

0 votes

In my Node module I have the following code:

throw new Error("Parsing is not possible");

Now I try to write a test which expects this error:

describe('my suite...', function() {
    [..]
    it('should not parse foo', function() {
    [..]
        expect(parser.parse(raw)).toThrow(new Error("Parsing is not possible"));
    });
});

I tried also Error() and some other variants and just can't figure out how to make it work

Jul 13, 2020 in Node-js by kartik
• 37,510 points
8,722 views

1 answer to this question.

0 votes

Hello @kartik,

Try using an anonymous function instead:

expect( function(){ parser.parse(raw); } ).toThrow(new Error("Parsing is not possible"));

you should be passing a function into the expect(...) call. Your incorrect code:

// incorrect:
expect(parser.parse(raw)).toThrow(new Error("Parsing is not possible"));

is trying to actually call parser.parse(raw) in an attempt to pass the result into expect(...),

Hope this is helpful!!
Thank you!!

answered Jul 13, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
0 answers

How to append to a file in Node?

I am trying to append a string to a ...READ MORE

Jul 9, 2020 in Node-js by kartik
• 37,510 points
769 views
0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,880 points
1,335 views
0 votes
1 answer

How to create an HTTPS server in Node.js?

Hello @kartik, The minimal setup for an HTTPS ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,880 points
1,526 views
0 votes
1 answer

How to redirect 404 errors to a page in ExpressJS?

Hello @kartik, You can put a middleware at ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,880 points
3,649 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,719 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,928 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
837 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
775 views
0 votes
1 answer

How to write files in Node.js?

Hello @kartik, Currently there are three ways to ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
703 views
0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
627 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP