spectator icon indicating copy to clipboard operation
spectator copied to clipboard

fix: override providers for createComponentFactory

Open stefanoww opened this issue 4 years ago • 6 comments

PR Checklist

Please check if your PR fulfills the following requirements:

  • [X] The commit message follows our guidelines: https://github.com/ngneat/spectator/blob/master/CONTRIBUTING.md#commit
  • [X] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

[X] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Overriding providers after executing createComponentFactory is impossible.

Issue Number: N/A #337

What is the new behavior?

When user want to override providers, testing module is reinitialized with new providers.

Does this PR introduce a breaking change?

[ ] Yes
[X] No

Other information

stefanoww avatar Jan 23 '21 08:01 stefanoww

@NetanelBasal I am not sure whether that is the best approach. From that what I was able to figure out, we can not override providers after calling compileComponents. If we want to override it, we need to completely reset current configuration and create new one. That's what I have done.

I think similar problem may occur within other create-factory.ts files. If current solution will be fine I may also fix it in other places.

stefanoww avatar Jan 23 '21 08:01 stefanoww

@NetanelBasal any update with this one?

stefanoww avatar Feb 12 '21 10:02 stefanoww

I don't think we need to use resetTestingModule for every provider. The issue is only when using InjectionToken, right?

NetanelBasal avatar Feb 12 '21 13:02 NetanelBasal

To be honest based one the that answer from issue https://github.com/ngneat/spectator/issues/337#issuecomment-689657302 and linked there issue from Angular repo I am not sure that problem occur only for InjectionToken. I have understood that you just can not override providers (any) when TestBed is already instantiated.

stefanoww avatar Feb 12 '21 19:02 stefanoww

Now I'm not sure what's the issue. Works for me - https://github.com/ngneat/bind-query-params/blob/master/projects/ngneat/bind-query-params/src/lib/lib.spec.ts#L115

NetanelBasal avatar Feb 13 '21 17:02 NetanelBasal

Maybe to better figure out that issue you may get into my branch and play with code and tests. What I understand as the problem there is that we just cannot override providers when the TestBed.compileComponents() is called and that what's mostly happen in beforeach block where we execute createComponentFactory.

stefanoww avatar Feb 23 '21 18:02 stefanoww