title: Testing catch promise in Jasmine and AngularJS
body: <p>I have some code like this inside my component:</p>
  
  <pre><code>p.a().then(function(x) {
    vm.x = x;
    return p.b();
  }).then(function(y) {
    if (!y) {
      return $q.reject(new Error('My Error'));
    }
    vm.y = y;
    return y;
  }).catch(function(error) {
    log.error(error);
  });
  </code></pre>
  
  <p>I'm able to test the success case fine:</p>
  
  <pre><code>it('is successful', function(done) {
    spyOn(p, 'a').and.returnValue($q.resolve('x'));
    spyOn(p, 'b').and.returnValue($q.resolve('y'));
  
    $ctrl = $componentController('myComponent', {
      $scope: $rootScope.$new()
    });
  
    p.a().then(function() {
      expect($ctrl.x).toEqual('x');
      return p.b();
    }).then(function() {
      expect($ctrl.y).toEqual('y');
      done();
    });
  
    $timeout.flush();
  });
  </code></pre>
  
  <p>But I am not able to test the <code>catch</code> and assert the error:</p>
  
  <pre><code>it('fails', function(done) {
    spyOn(p, 'a').and.returnValue($q.resolve());
    spyOn(p, 'b').and.returnValue($q.resolve());
  
    $ctrl = $componentController('myComponent', {
      $scope: $rootScope.$new()
    });
  
    p.a().then(function() {
      return p.b();
    }).catch(function(error) {
      expect(error).toEqual(new Error('My Error'));
      done();
    });
  
    $timeout.flush();
  });
  </code></pre>
  
  <p>All I get when I run the tests is:</p>
  
  <blockquote>
    <p>Error: Timeout - Async callback was not invoked within timeout
    specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.</p>
  </blockquote>
  
  <p>Which means the <code>catch</code> is not even running (I tried logging something to double check).</p>
  
  <p>Any ideas what am I doing wrong?</p>