Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine toThrowError fail, message "Error: Actual is not a function"

I'm building a Jasmine spec and writing toThrowError test.

 it("Should give time travel error", () => {
        const errorMsg = Logger.getLogsBetweenDates( { 
              fromDate : new Date("2017-01-06"),
              toDate : new Date("2017-01-05")});

        expect(errorMsg).toThrowError("Time travel error: fromDate must be befor toDate");
});

And im getting "Error: Actual is not a function" with no extra details.

  • What is that Actual?
  • The Logger.getLogsBetweenDates function is acutely throw the error, and the test is always fail. what am i doing wrong?
like image 950
Elad Avatar asked Jan 18 '18 13:01

Elad


1 Answers

What is that Actual?

As its name suggest, the Actual is the variable that contain the actual result from the tested function. what's your tested function actually returned.

Then, Jasmine take that Actual value and compare it with your expect value. It's easier to understand when you see the source code here.

Its happen in the code becuse the Logger.getLogsBetweenDates is throw an error, and errorMsg get no result; so the Actual is undefined, and the expect function compare an undefined to the error message.

what am i doing wrong?

You need to call the tested function inside of the expect function, like so:

it("Should give time travel error", () => {    
     expect(() => {
             Logger.getLogsBetweenDates( { 
                  fromDate : new Date("2017-01-06"),
                  toDate : new Date("2017-01-05")})
            }).toThrowError("Time travel error: fromDate must be befor toDate");
});

As shown here.

like image 78
Elad Avatar answered Nov 03 '22 14:11

Elad