Josh Longerbeam
Josh Longerbeam

Reputation: 100

Jasmine expect toThrow not passing nor catching

Trying to use expect(fn).toThrow() in protractor.

The function (fn) I'm passing returns a promise (protractor.promise.defer) which protractor supposedly forces expect() to deal with properly.

Upon running, instead of catching the error and/or passing the test, it does neither.

I have tried using:

Everytime the expect fails and the error is still thrown and uncaught (exactly as the syntax says it will be thrown).

My hunches are:

Upvotes: 4

Views: 1739

Answers (2)

pdenes
pdenes

Reputation: 802

Chai as Promised might help to have a cleaner syntax, e.g. something like:

expect(fn_returning_promise()).to.eventually.be.rejectedWith(...)

Upvotes: 1

asa
asa

Reputation: 4040

Assuming your promise will be fulfilled/rejected later in time (async), you can't rely on the expect(function() {..}.toThrow() to work in that case.

What I would do is something like this (not used to protractor promises):

it('.....', function(done) {
    MyPromise(...)
        .then(... my normal process which should throw....)
        .then(function() { 
                   // Error not thrown by the process, so fail the test.
                   expect(true).toBe(false);
              },
              function(err) {
                   // Expected error thrown so pass the test.
                   done();
              });
});

Assuming this is how you catch errors with protractor promises ?

Some other implementation have a .catch() method you can chain.

Upvotes: 1

Related Questions