Chai as Promised documentation states as follows:
Notice: either return or notify(done) must be used with promise assertions.
And the examples on the site are as follows:
return doSomethingAsync().should.eventually.equal("foo"); doSomethingAsync().should.eventually.equal("foo").notify(done);
The thing is; I actually wrote a test using chai as promised without returning the promise. Like so:
it('should resolve user', function () {
$state.get(state).resolve.user(dataservice, {
userId: testUser.id
}).should.eventually.eq(testUser);
$rootScope.$apply();
});
And it works perfectly fine. I am sure it does as I change testUser to something else the test fails. Just like I expected. So I am not sure if I am doing something wrong here.
In fact, when I modified the code to return a promise, it failed with error "Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test." The modified code is below:
it('should resolve user', function () {
var promise = $state.get(state).resolve.user(dataservice, {
userId: testUser.id
}).should.eventually.eq(testUser);
$rootScope.$apply();
return promise;
});
A little confused here. It might have something to do with Angular $q. To make it clear, the function resolve.user returns a $q promise.
user(dataService, { ... })
work synchronously? (i.e. does it return immediately?) – Adda$rootScope.$apply()
. – Malathionthen
callback is never called, too. $q promises are tied to Angular digests, this allows them to be synchronous. Any other promises are asynchronous. So if this means that $q is not 'compliant', that's it. – Malathionthen
callback should be called. – Padovathen
after$rootScope.$apply()
was called. So chainedthen
needs another$rootScope.$apply()
to be executed. Again, $q promises are synchronous, and Mocha's promise returns are meant for asynchronous specs. – Malathion