gomyes gomyes - 1 year ago 121
AngularJS Question

Do I really need to return a promise in test when using Chai as Promised?

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");


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

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
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.

Answer Source

In the case above Mocha chains returned promise after $rootScope.$apply() was called, so chained then needs another $rootScope.$apply() to be executed. Without this the rest of promise chain is not executed and results in timeout.

Returning promises in Mocha specs is intended for asynchronous specs, this is necessary for testing non-Angular promises. $q promises are synchronous and tied to Angular digests.

As shown here, chai-as-promised can be modified to support $q promises and apply $rootScope.$apply() automatically to asserted promises:

chaiAsPromised.transferPromiseness = function (assertion, promise) {
  assertion.then = promise.then.bind(promise);

  if (!('$$state' in promise))

  inject(function ($rootScope) {
    if (!$rootScope.$$phase)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download