question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
fabio-bertonecommented, Dec 16, 2021

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 }
  }
);
6reactions
LorenzSLAcommented, Nov 29, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unhandled promise rejection: Error: Injector has already been ...
Using PrimeNG as component library I got this problem in tests for components using any kind of overlay or popup (e.g. ...
Read more >
Improving Angular tests by enabling Angular testing module ...
Component-level services are destroyed 1 time less than the number of tests. The two first issues have the biggest impact when using Karma...
Read more >
Angular Test Fails At Injector? - ADocLib
When running ng test codecoverage keep getting this error Unhandled promise rejection: Error: Injector has already been destroyed.over.
Read more >
an error was thrown in afterall angular 12 - You.com | The AI ...
When running "ng test --code-coverage", keep getting this error "Unhandled promise rejection: Error: Injector has already been destroyed.".
Read more >
Component testing scenarios - Angular
A component-under-test doesn't have to be injected with real services. ... The component injector is a property of the fixture's DebugElement .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found