Skip to content

Jasmine unit tests throwing "Possibly unhandled rejection" with Angular 1.6?

Each $q.reject() throws an error if not being chained properly. First check that all calls have a .catch(). Second, if possible, isolate your error cases to an independent describe block. Example below using bardjs, but the idea should be clear enough.

  let stuffDataservice;
  const stuff = [{}]; 

  beforeEach(function() {

    [...]

    stuffDataservice = {
      getStuff: function () { return $q.when(stuff); },
      getSomethingElse: function () { return $q.when(somethingElse); }
    });

    scope = $rootScope.$new();

    controller = $controller('MyController', {
      $scope: scope,
      stuffDataservice: stuffDataservice
    });

  });
  describe('.getStuff()', function() {

    it('gets stuff', function() {
      scope.$apply();
      expect(controller.stuff).toBe(stuff);
    });

    describe('failure', function() {

      let failingController;

      beforeEach(function() {
        /* overrides previously defined $q.when */
        stuffDataservice.getStuff = function() {
          return $q.reject();
        };
        /* new controller needed to make use of changes in dataservice */
        failingController = $controller('MyController', {
          $scope: scope
        });
      });

      it('is handled', function() {
        scope.$apply();
        expect(failingController.variableDescribingTheFailure).toBeDefined();
      });

    });