Jasmine expects to fail and catch

Trying to use expect(fn).toThrow()in the transporter.

The function (fn)I pass returns a promise ( protractor.promise.defer), which the protractor claims to make it expect()work properly.

At startup, instead of catching an error and / or passing the test, it does not.

  • "Expected function to throw an error"
  • "Waiting expires in ~ 3000 ms"
    • (selected by timeout parameter browser.wait())

I tried using:

  • deferred.reject()that throws an exciting mistake
    • Usually caught using .thenCatch()instead of .then()in a promising function
  • deferred.fulfill() and throwing an error into the function "manually"
    • Using syntax throw new Error();
    • and throw {name: 'Error', message: 'timed out'}
    • and throw {name: 'Exception'}
  • .fulfill() .reject(), , - .
  • return, expect , .
  • : .toThrowError() .toThrow()
  • : .toThrow() , , .thenCatch(), result, .

, ( , , ).

hunches:

  • expect?
  • , throw , .then(function(){ /*error thrown here*/ }), fn?
  • , ? ( promises?) .toThrow(), .
  • - ?
+4
2

, / (async), expect(function() {..}.toThrow() .

, ( 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();
              });
});

, promises?

.catch(), .

+1

Chai as Promised , . - :

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

Source: https://habr.com/ru/post/1599376/


All Articles