I have hit a strange thing when trying to test a service returning ordinary $q promise. No promise I try in any test is actually being ever resolved/rejected (more specifically handlers from then
are not called, code inside promise runs just fine). I even tried forcing digest on a root scope, as some other answers on SO suggested, without any luck.
Here is a small self-contained example:
describe('promise', function(){
jasmine.DEFAULT_TIMEOUT_INTERVAL = 500;
let q;
beforeEach(inject(function($q){
q = $q;
}));
it('finishes', function(done){
expect(q).toBeDefined();
const promise = q.resolve();
console.log(promise);
promise.then(
() => done(),
() => done.fail()
);
});
});
What do I have to do to get the promise work as expected?
You need to use $scope.$apply() (or $rootScope.$digest()) and define then() before calling it. I've modified your example:
describe('promise', function(){
jasmine.DEFAULT_TIMEOUT_INTERVAL = 500;
let q;
let scope;
beforeEach(inject(function($q, $rootScope){
q = $q;
scope = $rootScope.$new();
}));
it('finishes', function(done){
expect(q).toBeDefined();
const promise = q.resolve();
promise.then(
() => done(),
() => done.fail()
);
scope.$apply();
});
});