8000 Bug: NG205 Injector has already been disposed · Issue #11324 · help-me-mom/ng-mocks · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Bug: NG205 Injector has already been disposed #11324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cheng93 opened this issue Apr 9, 2025 · 14 comments · Fixed by #11610
Open

Bug: NG205 Injector has already been disposed #11324

cheng93 opened this issue Apr 9, 2025 · 14 comments · Fixed by #11610
Labels
bug Something isn't working released v14.13.5

Comments

@cheng93
Copy link
Contributor
cheng93 commented Apr 9, 2025

Description of the bug

Getting the error in the title

An example of the bug

Image

Expected vs actual behavior

ng-mocks: 14.13.2 https://stackblitz.com/edit/github-vatrfd7a - works
ng-mocks: 14.13.3 https://stackblitz.com/edit/github-vatrfd7a-6lzfrewh - does not work
ng-mocks: 14.13.3 https://stackblitz.com/edit/github-vatrfd7a-jb25u2dw - attempt to not destroy after each - does not work

@cheng93 cheng93 added the bug Something isn't working label Apr 9, 2025
@cheng93
Copy link
Contributor Author
cheng93 commented Apr 9, 2025

I assume it's because of #10825

@cheng93
Copy link
Contributor Author
cheng93 commented Apr 9, 2025

https://stackblitz.com/edit/github-vatrfd7a-tjr14249

looks like after MockRender, shouldTearDownTestingModule() changes from false, to true

@dhutaryan
Copy link

Faced the same issue

@rasulovdev
Copy link

Same here

@dhutaryan
Copy link

@satanTime I would appreciated if you publish this fix

@satanTime
Copy link
Member

100%

@dhutaryan
Copy link

I'm really looking forward to it 😄 because I'm just now encountering this problem again on another project

@satanTime
Copy link
Member

v14.13.5 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

@dhutaryan
Copy link

For me, unfortunately, nothing has changed. Let's wait until author of this ticket check it.

@cheng93
Copy link
Contributor Author
cheng93 commented May 22, 2025

This has fixed my issue.

Remember you'll need to tell the TestBed that you do not want to destory.

const built = MockBuilder(TestComponent).build();
TestBed.configureTestingModule({
    ...built,
    teardown: { destroyAfterEach: false },
});
return await TestBed.compileComponents();

@satanTime satanTime reopened this May 22, 2025
@satanTime
Copy link
Member

Good. I'll take a look deeper once I've got a spare moment.
feel free to flood here with examples of failing tests which I could investigate.

@dhutaryan
8000
Copy link

So, I use jest, don't configure TestBed at all and don't use it anywhere.

@cheng93 actually, you example is working but as I mentioned above we don't use TestBed inside tests

@dhutaryan
Copy link

One more thing, it happened after upgrade Angular from 17 to 19 version and upgrade ng-mocks as well

@cheng93
Copy link
Contributor Author
cheng93 commented May 28, 2025

I believe ng-mocks configures TestBed under the hood, you are in fact, even though you're not calling in your code, using it.

I believe

  • 14.13.2 - TestBed was not getting reset at all, thus injector is still present
  • 14.13.3 - TestBed is always getting reset (regardless of TestBed setting), thus injector will get disposed all the time
  • 14.13.5 - TestBed respects setting, so if you have it configured not to reset, it will not dispose, thus injector will still be there.

I have provided a sample on how to configure TestBed, however it looks like you want ng-mocks to introduce a new API to configure whether we should teardown the testing module or not.

I think to get 14.13.2 behaviour you can also do the following somewhere (test.ts?)

getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(),
  {
    teardown: {
      destroyAfterEach: false
    }
  }
);

Disclaimer: Everything above could be wrong 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released v14.13.5
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0