spectator
spectator copied to clipboard
Overriding Injection Tokens not working for createComponentFactory
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x ] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:
Current behavior
I am trying to override a provider's value that's tied to an InjectionToken with no luck.
This is my createComponentFactory function where I'm setting up the providers.
Here, in my test where I'm trying to override that same provider.
This is the result. Nothing had changed.
Expected behavior
The provider should be overriden.
Environment
Angular version: 10.0.8
Browser:
- [ x ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: 10.20.1
- Platform: Windows 10
Others:
Please provide a reproduction example.
@NetanelBasal here's a quick example of that.
https://github.com/Kurtz1993/spectator-issue
@Kurtz1993 in this test case
it('should get the mocked instance of STATE injection token', () => { ... })
you are trying to recreate component when TestBed is already initialized under the hood by
beforeEach(() => { spectator = createComponent(); });
It's an inconsistency on the Angular side, because it throws error when using override... methods after TestBest initialization, but not in overrideProvider case (neither throws an error nor override provider). I have created pull request which should fix this issue in Angular repository https://github.com/angular/angular/pull/38717
I have updated to angular 11 where the issue is fixed (now throwing an error if attempting to override providers) and I think that makes spectator code to fail. I think the issue is here: https://github.com/ngneat/spectator/blob/master/projects/spectator/src/lib/spectator/create-factory.ts#L96 so I assume spectator does not officially support angular 11?
I might take that one if it's still open.
@stefanoww you can take it.
The problem is still relevant. Now I am creating a new createComponentFactory
in each describe
with different providers. Please fix it.