From 6bbc86588e36b330076bb9bca1658c7192379922 Mon Sep 17 00:00:00 2001 From: Michael Giambalvo Date: Thu, 21 Mar 2019 20:27:32 -0700 Subject: [PATCH] fix(ngMock): don't clean up if currentSpec is null In unit tests that use Angular and AngularJS, sometimes an error being thrown during the test and cause this afterEach hook to run twice. If that happens, on the second run cleanup will throw an error, since at that point currentSpec is null. This causes the original error to be hidden by a "TypeError: Cannot read property '$injector' of null while testing" error. This makes cleanup safe to call multiple times, working around the issue. --- src/ngMock/angular-mocks.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 4599578f1283..c9a59e84e862 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -3000,6 +3000,9 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) { }; module.$$cleanup = function() { + if (!currentSpec) { + return; + } var injector = currentSpec.$injector; annotatedFunctions.forEach(function(fn) {