Jeanluca Scaljeri Jeanluca Scaljeri - 6 months ago 74
AngularJS Question

Promise.resolve().then not working in jasmine test

I'm trying to setup a test which involves promises. Here is my example code:

var promise;

beforeEach(inject(function ($q) {
promise = $q.resolve();
}));

it('should resolve', function (done) {
promise.then(function () {
expect(true).toBeTruthy();
done();
});
});


DEMO

For some reason, when I run this, I get a TIMEOUT

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.


Why doesn't the promise execute the callback given to
then
?

Cheers

Answer

You need to call scope/rootScope $digest method to resolve promises. So it should be:

var result = false;
promise.then(function() { result = true;});
$rootScope.$digest;
expect(result).toBeTruthy();
Comments