I'm currently facing a challenge in unit testing the handling of external promises being resolved or rejected.
The issue arises when using the ngMock
module, as it seems to prevent these promises from ever completing. This is particularly problematic for my tests involving HTTP requests and timeouts.
For instance, consider the situation with angular-pouchdb, an essential dependency in my project. Due to the utilization of PouchDB promises after version 2.0.0, the tests remain stuck without finishing when ngMock is included.
An example snippet, derived from angular-pouchdb test cases:
var scope;
beforeEach(function() {
var $injector = angular.injector(['ng', 'ngMock', 'pouchdb']);
var pouchDB = $injector.get('pouchDB');
scope = $injector.get('$rootScope');
db = pouchDB('db');
});
it('should wrap destroy', function(done) {
db.destroy()
.then(shouldBeOK)
.catch(shouldNotBeCalled)
.finally(done);
// Despite attempting multiple methods like $apply, $applySync, $timeout.flush, etc., the tests simply do not finish.
scope.$apply();
});
The output for the aforementioned test appears as follows:
C:\Users\Gustavo\Projetos\main\angular-pouchdb (master)
λ karma start
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Windows 8 0.0.0)]: Connected on socket s1A7jIzmtcOxkGCtzYke with id 42899233
PhantomJS 1.9.8 (Windows 8 0.0.0) Angular-aware PouchDB public API should wrap destroy FAILED
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Hence, my main inquiry revolves around: Could there be something crucial that I am overlooking in Angular's unit testing process? Or perhaps, might this issue stem from an internal bug within ngMock?
An issue regarding this matter has also been raised in the angular-pouchdb repository.