angular: "ng test" : Unhandled promise rejection: Error: Injector has already been destroyed.

Which @angular/* package(s) are the source of the bug?

Don’t known / other

Is this a regression?

No

Description

When running “ng test --code-coverage”, keep getting this error “Unhandled promise rejection: Error: Injector has already been destroyed.”

over karma dashboard if keep refreshing, this error goes away and code coverage appears.

i am not sure what is this error and where to look for what because error is not telling where is the problem, Spent number of days over it, please suggest if its angular version issue and if there is any patch available for now

attached screen shot as well error log.

karmaError

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Chrome 96.0.4664.45 (Windows 10): Executed 23 of 24 SUCCESS (0 secs / 0.282 secs)
Chrome 96.0.4664.45 (Windows 10) ERROR
  An error was thrown in afterAll
  Unhandled promise rejection: Error: Injector has already been destroyed.
  Error: Injector has already been destroyed.
      at R3Injector.assertNotDestroyed (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11342:19)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11270:14)
      at NgModuleRef.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:22189:33)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11306:33)
      at injectInjectorOnly (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4774:33)
      at Module.ɵɵinject (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4778:12)
      at Object.factory (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/router/fesm2020/router.mjs:5893:1)
      at R3Injector.hydrate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11476:35)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11295:33)
      at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11333:55
  Unhandled promise rejection: Error: Injector has already been destroyed.
  Error: Injector has already been destroyed.
      at R3Injector.assertNotDestroyed (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11342:19)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11270:14)
      at NgModuleRef.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:22189:33)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11306:33)
      at injectInjectorOnly (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4774:33)
      at Module.ɵɵinject (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4778:12)
      at Object.factory (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/router/fesm2020/router.mjs:5893:1)
      at R3Injector.hydrate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11476:35)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11295:33)
      at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11333:55
Chrome 96.0.4664.45 (Windows 10): Executed 24 of 24 ERROR (0 secs / 0.284 secs)
Chrome 96.0.4664.45 (Windows 10) ERROR
  An error was thrown in afterAll
  Unhandled promise rejection: Error: Injector has already been destroyed.
  Error: Injector has already been destroyed.
      at R3Injector.assertNotDestroyed (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11342:19)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11270:14)
      at NgModuleRef.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:22189:33)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11306:33)
      at injectInjectorOnly (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4774:33)
      at Module.ɵɵinject (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4778:12)
      at Object.factory (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/router/fesm2020/router.mjs:5893:1)
      at R3Injector.hydrate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11476:35)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11295:33)
      at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11333:55
  Unhandled promise rejection: Error: Injector has already been destroyed.
  Error: Injector has already been destroyed.
      at R3Injector.assertNotDestroyed (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11342:19)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11270:14)
      at NgModuleRef.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:22189:33)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11306:33)
      at injectInjectorOnly (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4774:33)
      at Module.ɵɵinject (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:4778:12)
      at Object.factory (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/router/fesm2020/router.mjs:5893:1)
      at R3Injector.hydrate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11476:35)
      at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2020/core.mjs:11295:33)
Chrome 96.0.4664.45 (Windows 10): Executed 24 of 24 ERROR (0.36 secs / 0.284 secs)

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 13.0.2
Node: 14.16.0
Package Manager: npm 8.1.0
OS: win32 x64

Angular: 13.0.1
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1300.2
@angular-devkit/build-angular   13.0.2
@angular-devkit/core            13.0.2
@angular-devkit/schematics      13.0.2
@angular/cdk                    12.2.13
@angular/cli                    13.0.2
@angular/material               12.2.13
@schematics/angular             13.0.2
rxjs                            6.6.7
typescript                      4.4.4

Anything else?

Please let know if there any resolution of this issue.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 24 (3 by maintainers)

Most upvoted comments

Not sure if this’ll help but I finally got the tests to pass!! I played around by add { teardown: { destroyAfterEach: false } } to test.ts file and it started working again. Looking into this option, it seems with v13, it was defaulted to true which would explain the behavior between versions. Hope this helps.

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(),
  {
    teardown: { destroyAfterEach: false }
  }
);

I am having the same problem. It started after updating to Angular 13. The error does not occur on every test run but seems to pop-up randomly.

I have tried with destroyAfterEach: true but still the issue isn’t solved and also tried to upgrade the project from angular 12 to 13, the result is same, so is there any other alternatives to solve this issue?

Same thing for me, Angular 13, error which pops up randomly on tests.

Same Problem with 12.2.1

I have a little bit different issue after upgrade to angular 13: Unhandled promise rejection: ObjectUnsubscribedError: object unsubscribed However, adding teardown: { destroyAfterEach: true } worked for me.

Our tests start working again after we stop importing the following Angular material modules: SnackbarModule and MatDialogModule. Instead, we now inject mock services.

We kept on test.ts:

teardown: { destroyAfterEach: true }

No @ducthang-vu , i am still looking for solution, its very strange to me that i am not getting resolution of this error anywhere.

i am not able to upload minimal set of code as per my company policy to reproduce, if you are facing same issue, then can you upload the code ?