The statement about $q
synchronicity applies to unit tests with ngMock
in the first place.
$q
promises are capable of being synchronous in production:
let foo;
$q.resolve().then(() => { foo = 1 });
$rootScope.$digest();
console.log(foo === 1);
And they are supposed to be synchronous in unit tests, because all AngularJS services that are responsible for asynchronous behaviour ($timeout
, $http
, etc) are mocked with ngMock in order to make tests fully synchronous:
it('...', inject(($q) => {
let foo;
$q.resolve().then(() => { foo = 1 });
$rootScope.$digest();
expect(foo).toBe(1);
}));
While ES6 promises are asynchronous by design, and then
callback runs on next tick:
it('...', (done) => {
let foo;
Promise.resolve(1).then(() => {
foo = 1;
expect(foo).toBe(1);
})
.then(done, done.fail);
});