AngularJS not being executed in unit tests

Unit testing in Angular can be tricky because of the digest cycle. Your test code might have multiple references to $scope.apply(); in order to get promises moving.

Recently I was testing something that happened after a $timeout(). In the unit test file, I was unable to see the results that I expected. I tried $scope.apply() but that didn’t do anything.

After viewing the $timeout documentation, the answer was right there…

In tests you can use $timeout.flush() to synchronously flush the queue of deferred functions.

So that’s it… $timeout.flush() in your unit test… FTW.

avatar

Dev Blog