3

I have a function that throws an error that I am testing. Here is the function:

parse: function(input) {
        var results = {};       
        var that = this;
        input.forEach(function(dependency) {
            var split = dependency.split(": ");
            var lib = split[0];
            var depen = split[1];       
            if(depen === undefined) {
                throw new Error('Invalid input. Requires a space after each colon.')
            }
            results[lib] = depen;
        });
        return results;
    }

When I test this function, I hit the error code and want to validate that an error is being thrown. Here is my test code:

var invalidInput = ['Service1: ', 'Service2:stuff'] 
expect(manager.parse(invalidInput)).toThrowError();

But my test fails. Here is my stack trace:

Failures:

  1) dependency.js input should require a space after each colon as specified by requirements
   Message:
     Error: Invalid input. Requires a space after each colon.
   Stacktrace:
     Error: Invalid input. Requires a space after each colon.
    at /Users/name/Development/sight/dependency.js:49:11
    at Array.forEach (native)
    at Object.module.exports.parse (/Users/name/Development/sight/dependency.js:44:9)
    at null.<anonymous> (/Users/name/Development/sight/spec/dependency.spec.js:34:12)

I am using jasmine-expect to test for the error being thrown. What am I doing wrong?

jhamm
  • 24,124
  • 39
  • 105
  • 179

2 Answers2

7

You need to pass a function as parameter to expect in conjunction with toThrow or toThrowError.

var invalidInput = ['Service1: ', 'Service2:stuff'] 
expect(function () { manager.parse(invalidInput); }).toThrow();

or

var invalidInput = ['Service1: ', 'Service2:stuff'] 
expect(function () { manager.parse(invalidInput); }).toThrowError(Error);
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
0

Seens toThrowError to require arguments. Try to set arguments: it can be an expected error message or type, or both.

expect(manager.parse(invalidInput)).toThrowError('Invalid input. Requires a space after each colon.');

For ignoring error message you may use toThrow. From documentation:

it("The 'toThrow' matcher is for testing if a function throws an exception", function() {
    var foo = function() {
      return 1 + 2;
    };
    var bar = function() {
      return a + 1;
    };

    expect(foo).not.toThrow();
    expect(bar).toThrow();
  });

  it("The 'toThrowError' matcher is for testing a specific thrown exception", function() {
    var foo = function() {
      throw new TypeError("foo bar baz");
    };

    expect(foo).toThrowError("foo bar baz");
    expect(foo).toThrowError(/bar/);
    expect(foo).toThrowError(TypeError);
    expect(foo).toThrowError(TypeError, "foo bar baz");
  });
});
phts
  • 3,889
  • 1
  • 19
  • 31
  • I used your `expect` statement and got the same error. – jhamm Apr 09 '15 at 14:40
  • `toThrow` works for me but when I am using `toThrowError` then I am always getting: "toThrowError is not a function". Any idea where this comes from? – Benny Code Oct 20 '15 at 00:53