0

I have recently updated from 1.5 to 1.6.3 and have code where I am using deferred promise objects using the $q service which is now throwing the following errors in my unit test runs:

Possibly unhandled rejection: undefined thrown

Here is the typical structure of a function I have in my code that uses a deferred promise:

public someFunction = () => {
    const deferred = this.$q.defer();
    if (someConditionIsTrue) {
        this.myService.getThings().then((response) => {
            deferred.resolve(response);
        });
    } else {
        deferred.reject();
    }
    return deferred.promise;
};

When unit testing the else condition I get this error thrown (using karma\jasmine\phantomJS)

I already have the following configured also:

config(['$qProvider', ($qProvider) => {
        $qProvider.errorOnUnhandledRejections(false);
      }])

Can someone point me in the right direction please?

Thanks

mindparse
  • 6,115
  • 27
  • 90
  • 191
  • Can you try passing an argument inside `deferred.reject()` like `deferred.reject(false)` – codtex Mar 13 '17 at 10:15
  • I tried with your suggestion and still get the error, only this time it says `Possibly unhandled rejection: false thrown` previously it was saying `undefined thrown` – mindparse Mar 13 '17 at 10:21
  • 1
    Yes of course, now I understand. You are getting this error because you don't have a function to handle your rejection like this `somePromise.then(function(){/*this is resolve function*/}, function(){/*this is reject function*/})`. Let me know if this is the reason and I can post an answer – codtex Mar 13 '17 at 10:24
  • Ok I see what you are saying, so does this mean from where I am calling this function in my test spec I will need to handle it like this? – mindparse Mar 13 '17 at 10:44
  • yes, try handling rejection inside the tests, too – codtex Mar 13 '17 at 10:46
  • Hmm ok, shouldnt the `$qProvider.errorOnUnhandledRejections(false);` config prevent me from having to do this?? – mindparse Mar 13 '17 at 10:47
  • Well the name of the method `errorOnUnhandledRejections` says that it should, but it will cost nothing to test – codtex Mar 13 '17 at 10:48
  • Sure ok, well using that notation in the test has certainly removed the error, I wonder if angular mocks 1.6.3 is not behaving correctly with that config set - thanks for your help anyway :) – mindparse Mar 13 '17 at 11:19
  • There might be a possibility that this row of code `$qProvider.errorOnUnhandledRejections(false);` is not configured on the right place and that's why it is not working, otherwise there is a problem in angular.js code. Good Luck! – codtex Mar 13 '17 at 11:25
  • Possible duplicate of [Unable to test a rejected promise after migrating to Angular 1.6.3](http://stackoverflow.com/questions/42733618/unable-to-test-a-rejected-promise-after-migrating-to-angular-1-6-3) – georgeawg Mar 13 '17 at 14:14

1 Answers1

0

Angular is suggesting you to handle every possible route of the promise.

Currently you are missing this part:

public someFunction = () => {
    const deferred = this.$q.defer();
    if (someConditionIsTrue) {
        this.myService.getThings().then((response) => {
            deferred.resolve(response);
        });
        // what happen if this promise is rejected?
    } else {
        deferred.reject();
    }
    return deferred.promise;
};

While you can add a .catch on the commented part, I would suggest changing the whole structure like this for cleaner code and easier maintenance:

public someFunction = () => {
    if (someConditionIsTrue) {
        return this.myService.getThings();
        // this will pass back whatever resolve/reject to upper layer
    } else {
        return $q.reject();
    }
};
Icycool
  • 7,099
  • 1
  • 25
  • 33