Using toThrowError in Jasmine

I am working on an application that makes heavy use of JavaScript. I need unit test this code. Trying to do this, I rely on Jasmine.

Some of my JavaScript code calls Error JavaScript objects. These objects assign values ​​to the message and name attribute of the Error object. I assign an exception type to the name property. For example, sometimes the name has the value "OutOfRangeException", sometimes its "ArgumentException", etc.

How to use the toThrowError function in a Jasmine structure to check if an abandoned error has a specific name? Currently, my JavaScript looks like this:

function getRandomNumber(max) { if ((!isNaN(parseFloat(max)) && isFinite(max)) === false) { var error = new Error('You must provide a number'); error.name = 'ArgumentException'; throw error; } if ((max === null) || (max < 1) || (max > 100)) { var error = new Error('The maximum value must be greater than 0 and less than 100.'); error.name = 'ArgumentOutOfRangeException'; throw error; } return Math.floor(Math.random() * max) + 1; } function ArgumentException(message) { this.name = 'ArgumentException'; this.message = message || ''; } ArgumentException.prototype = new Error(); ArgumentException.prototype.constructor = ArgumentException; 

How can I write a Jasmine test that checks for an ArgumentException error or an ArgumentOutOfRangeException error?

Thanks!

+8
unit-testing jasmine
source share
1 answer

Exception checking for a function with a parameter is not supported in jasmine. But you can use the workaround below to overcome this limitation and test your functions.

 describe('toThrowError test case', function() { it('test getRandomNumber function for undefined', function() { expect(function() { getRandomNumber(undefined); }).toThrowError("You must provide a number"); }); it('test getRandomNumber function for 0', function() { expect(function() { getRandomNumber(0); }).toThrowError("The maximum value must be greater than 0 and less than 100."); }); }); 

toThrowError allows toThrowError to select one or two parameters

  • 1 Parameter - exception or type of exception
  • 2 Parameters - Type of exception and exception message

An example of checking based on the type of exception:

 function getRandomNumber(max) { throw new SyntaxError(); } describe('toThrowError test case', function() { it('test getRandomNumber function for undefined', function() { expect(function() { getRandomNumber(undefined); }).toThrowError(SyntaxError); }); }); 

Refer link for different types of exceptions.

Custom error message

The following snippet provides a sample for using custom error messages.

 function getRandomNumber(max) { throw new ArgumentException(); } function ArgumentException(message) { this.name = 'ArgumentException'; this.message = message || ''; } ArgumentException.prototype = new Error(); ArgumentException.prototype.constructor = ArgumentException; describe('toThrowError test case', function() { it('test getRandomNumber function for undefined', function() { expect(function() { getRandomNumber(undefined); }).toThrowError(ArgumentException); }); }); 
+21
source share

All Articles