{"record":{"id":"179c6bf2546f82cd","repo":"angular/angular.js","slug":"deferred-tasks-to-flush","errorCode":null,"errorMessage":"Deferred tasks to flush ({}):\n  {}","messagePattern":"Deferred tasks to flush \\((.+?)\\):\n  (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/ngMock/angular-mocks.js","lineNumber":199,"sourceCode":"  };\n\n  /**\n   * @name $browser#defer.verifyNoPendingTasks\n   *\n   * @description\n   * Verifies that there are no pending tasks that need to be flushed.\n   * You can check for a specific type of tasks only, by specifying a `taskType`.\n   *\n   * See {@link $verifyNoPendingTasks} for more info.\n   *\n   * @param {string=} taskType - The type tasks to check for.\n   */\n  self.defer.verifyNoPendingTasks = function(taskType) {\n    var pendingTasks = self.defer.getPendingTasks(taskType);\n\n    if (pendingTasks.length) {\n      var formattedTasks = self.defer.formatPendingTasks(pendingTasks).join('\\n  ');\n      throw new Error('Deferred tasks to flush (' + pendingTasks.length + '):\\n  ' +\n          formattedTasks);\n    }\n  };\n\n  self.$$baseHref = '/';\n  self.baseHref = function() {\n    return this.$$baseHref;\n  };\n};\nangular.mock.$Browser.prototype = {\n\n  /**\n   * @name $browser#poll\n   *\n   * @description\n   * run all fns in pollFns\n   */\n  poll: function poll() {","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/angular/angular.js/blob/d8f77817eb5c98dec5317bc3756d1ea1812bcfbe/src/ngMock/angular-mocks.js#L181-L217","documentation":"$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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before\nafterEach(inject(function($verifyNoPendingTasks) {\n  $verifyNoPendingTasks(); // throws: leftover $interval from poller\n}));\n\n// after\nafterEach(inject(function($verifyNoPendingTasks) {\n  $verifyNoPendingTasks('$timeout'); // only assert timeout tasks\n}));\n// and in the test: $interval.flush(1000) or poller.stop() to clear intervals","handlingStrategy":"validation","validationCode":"// Scope verification to the task type you actually care about\ninject(function($verifyNoPendingTasks) {\n  $verifyNoPendingTasks('$timeout'); // only throws for pending $timeout tasks\n});\n\n// Or drain tasks before asserting\ninject(function($browser) {\n  if ($browser.defer.getPendingTasks('$timeout').length) {\n    $browser.defer.flush();\n  }\n});","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["angular-mocks","timeout","interval","pending-tasks","testing"],"backgroundTag":"pending-timers-not-flushed","analyzedSha":"d8f77817eb5c98dec5317bc3756d1ea1812bcfbe","analyzedAt":"2026-08-21T20:36:31.651Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}