I have a segment of code inside a $timeout which executes a promise. When i write the unit test for that i use $timeout.flush() and i can go inside the $timeout block. The promise is then resolved (since $timeout calls $scope.$apply() internally) but then after the method code finishes and bubbles up to jasmine i get the error TypeError: Cannot read property '$$nextSibling' of undefined. Anyone else experienced this before?
Controller Code:
$scope.timeoutFunction = function(){
$timeout(function () {
$scope.callFunction().then(function () {
callAnotherFunction();
});
}, 0);
}
Unit test code:
$scope.timeoutFunction();
scope.$digest(); // not needed
$timeout.flush(); // even tried $timeout.flush(0);
Also if i mock the timeout to be a callback function it works. Example:
function mockedTimeout() {
arguments[0]();
}
$timeout = mockedTimeout;
Then we just use the $scope.$digest for the promises to be called, timeout runs immediately. Could it be angular bug?
Complete error stack:
TypeError: Cannot read property '$$nextSibling' of undefined
at n.$get.n.$digest (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\Libs\Angular\angular.min.js:124:101)
at n.$get.n.$apply (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\Libs\Angular\angular.min.js:126:293)
at Object.fn (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\Libs\Angular\angular.min.js:139:3)
at Function.angular.mock.$Browser.self.defer.flush (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\TDD\angular-mocks.js:127:42)
at Function.angular.mock.$TimeoutDecorator.$delegate.flush (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\TDD\angular-mocks.js:1732:28)
at null.<anonymous> (http://localhost:51679/Tests.js:254:18)
at jasmine.Block.execute (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:1064:17)
at jasmine.Queue.next_ (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:2096:31)
at jasmine.Queue.start (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:2049:8)
at jasmine.Spec.execute (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:2376:14)"