Elad
Elad

Reputation: 2387

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.

Upvotes: 13

Views: 8844

Answers (1)

Elad
Elad

Reputation: 2387

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.

Upvotes: 22

Related Questions