How to expect to trigger an specific error in Jasmine test

I am trying to write a jasmine test for an expression engine function. In the test I would like to test an edge case where the input is incorrect and will lead to a runtime error. What is the right way to implement this test such that it check if the resulting error match the expected error?

1 Like
var erroringFunction = function() { MyType.myFunc(); };
expect(erroringFunction).toThrowError(/A_PART_OF_THE_ERROR_MESSAGE/);

Note that you are likely to get an error with a long stack trace. Using // helps, because it can detect any match within the long stack trace

3 Likes
var erroringFunction = function() {OilGasWell.evalMetric({
      id: well.id,
      expression: "virtualSensor([ts1, ts2], target, keepMask, dateTime('2017-12-30'), 'linear')",
      interval: interval,
      start: start,
      end: end,
      bindings: bindings
    });};
expect(erroringFunction).toThrowError("/trainTestSplit should be greater than the start of the evaluation period/");

I have tried this approach but it did not pass the test. My code looks like above. Is there something wrong with my code?

This is a string that begins and ends with a / character.
Try a regular expression instead, more info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions