spectator icon indicating copy to clipboard operation
spectator copied to clipboard

Overriding Injection Tokens not working for createComponentFactory

Open Kurtz1993 opened this issue 4 years ago • 7 comments

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. image

Here, in my test where I'm trying to override that same provider. image

This is the result. Nothing had changed. image

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:

Kurtz1993 avatar Aug 18 '20 16:08 Kurtz1993

Please provide a reproduction example.

NetanelBasal avatar Aug 28 '20 14:08 NetanelBasal

@NetanelBasal here's a quick example of that.

https://github.com/Kurtz1993/spectator-issue

Kurtz1993 avatar Sep 06 '20 01:09 Kurtz1993

@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

adrianrutkowski avatar Sep 09 '20 15:09 adrianrutkowski

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?

bboydflo avatar Nov 23 '20 21:11 bboydflo

I might take that one if it's still open.

stefanoww avatar Jan 16 '21 13:01 stefanoww

@stefanoww you can take it.

NetanelBasal avatar Jan 16 '21 16:01 NetanelBasal

The problem is still relevant. Now I am creating a new createComponentFactory in each describe with different providers. Please fix it.

nikulin-anton avatar May 31 '21 06:05 nikulin-anton