angular/angular.js · error · Error
Deferred tasks to flush ({}): {}
Error message
Deferred tasks to flush ({}):
{} What it means
$browser.defer.verifyNoPendingTasks — exposed as the $verifyNoPendingTasks service — is an end-of-test assertion that the mock browser's deferred queue is empty. If any task remains ($timeout, $http, $animate, $browser deferred work), it formats each pending task's type and scheduled time and throws 'Deferred tasks to flush (N)' so the test cannot silently leak asynchronous work.
Source
Thrown at src/ngMock/angular-mocks.js:199
};
/**
* @name $browser#defer.verifyNoPendingTasks
*
* @description
* Verifies that there are no pending tasks that need to be flushed.
* You can check for a specific type of tasks only, by specifying a `taskType`.
*
* See {@link $verifyNoPendingTasks} for more info.
*
* @param {string=} taskType - The type tasks to check for.
*/
self.defer.verifyNoPendingTasks = function(taskType) {
var pendingTasks = self.defer.getPendingTasks(taskType);
if (pendingTasks.length) {
var formattedTasks = self.defer.formatPendingTasks(pendingTasks).join('\n ');
throw new Error('Deferred tasks to flush (' + pendingTasks.length + '):\n ' +
formattedTasks);
}
};
self.$$baseHref = '/';
self.baseHref = function() {
return this.$$baseHref;
};
};
angular.mock.$Browser.prototype = {
/**
* @name $browser#poll
*
* @description
* run all fns in pollFns
*/
poll: function poll() {View on GitHub (pinned to d8f77817eb)
Solutions
- Flush the work before verification: $timeout.flush() for timeouts, $interval.flush(millis) for intervals, $httpBackend.flush() for pending requests.
- Cancel what should not run: keep $timeout/$interval handles and cancel them in afterEach.
- Scope the check to one task type: inject $verifyNoPendingTasks and call it with '$timeout' (or '$http', '$animate', '$interval').
- Move setup that schedules long-lived timers out of the per-test injector (e.g., behind a flag the test controls).
Example fix
// before
afterEach(inject(function($verifyNoPendingTasks) {
$verifyNoPendingTasks(); // throws: leftover $interval from poller
}));
// after
afterEach(inject(function($verifyNoPendingTasks) {
$verifyNoPendingTasks('$timeout'); // only assert timeout tasks
}));
// and in the test: $interval.flush(1000) or poller.stop() to clear intervals Defensive patterns
Strategy: validation
Validate before calling
// Scope verification to the task type you actually care about
inject(function($verifyNoPendingTasks) {
$verifyNoPendingTasks('$timeout'); // only throws for pending $timeout tasks
});
// Or drain tasks before asserting
inject(function($browser) {
if ($browser.defer.getPendingTasks('$timeout').length) {
$browser.defer.flush();
}
}); Prevention
- Keep $timeout/$interval handles and cancel them in afterEach.
- Flush each subsystem you exercised ($timeout, $httpBackend, $animate) before verifying.
- Pass an explicit taskType to $verifyNoPendingTasks instead of asserting all types blindly.
When it happens
Trigger: Calling $verifyNoPendingTasks() (directly or via afterEach) while a $timeout/$interval callback is still queued; a test that creates a service in beforeEach which self-schedules a recurring $interval; an unflushed $http task created by the fake backend; passing no taskType so all deferred task types are counted.
Common situations: Adding afterEach(inject($verifyNoPendingTasks)) per the ngMock docs, then hitting leftover timers from a debounce/polling service initialized in setup; $interval-based services started but never stopped ($interval has no flush-all unless you flush(millis)); animations mocked via $animate leaving close tasks in the queue.
Related errors
- No deferred tasks to be flushed
- Expected $log to be empty! Either a message was logged unexp
- No pending animations ready to be closed or flushed
- No pending request to flush !
- No more pending request to flush !
AI-assisted analysis of angular/angular.js@d8f77817eb (2026-08-21).
Data as JSON: /api/errors/179c6bf2546f82cd.
Report an issue: GitHub.