Skip to content
Permalink
Browse files

fix(ivy): align TestBed.overrideProvider with what happens with provi…

…ders in TestBed providers array (#33769)

In Ivy, if you do:
`TestBed.configureTestingModule({providers: [{provide: Service}]});`
the injector will attempt to inject Service as if it was simply listed
in the providers array like `{providers: [Service]}`
This fixes an inconsistency when similarly providing an override with no
`useValue` or `useFactory`.

PR Close #33769
  • Loading branch information
atscott authored and AndrewKushnir committed Nov 12, 2019
1 parent 26dba21 commit 10a33efc3ea4400e67b7b4195f54a4bac2c8e3a9
Showing with 26 additions and 8 deletions.
  1. +13 −0 packages/core/test/test_bed_spec.ts
  2. +13 −8 packages/core/testing/src/r3_test_bed_compiler.ts
@@ -989,4 +989,17 @@ describe('TestBed', () => {
.toEqual(originalResolver);
});
});

onlyInIvy('VE injects undefined when provider does not have useValue or useFactory')
.describe('overrides provider', () => {
it('with empty provider object', () => {
@Injectable()
class Service {
}
TestBed.overrideProvider(Service, {});
// Should be able to get a Service instance because it has no dependencies that can't be
// resolved
expect(TestBed.inject(Service)).toBeDefined();
});
});
});
@@ -158,14 +158,19 @@ export class R3TestBedCompiler {
overrideProvider(
token: any,
provider: {useFactory?: Function, useValue?: any, deps?: any[], multi?: boolean}): void {
const providerDef = provider.useFactory ?
{
provide: token,
useFactory: provider.useFactory,
deps: provider.deps || [],
multi: provider.multi
} :
{provide: token, useValue: provider.useValue, multi: provider.multi};
let providerDef: Provider;
if (provider.useFactory !== undefined) {
providerDef = {
provide: token,
useFactory: provider.useFactory,
deps: provider.deps || [],
multi: provider.multi
};
} else if (provider.useValue !== undefined) {
providerDef = {provide: token, useValue: provider.useValue, multi: provider.multi};
} else {
providerDef = {provide: token};
}

const injectableDef: InjectableDef<any>|null =
typeof token !== 'string' ? getInjectableDef(token) : null;

0 comments on commit 10a33ef

Please sign in to comment.
You can’t perform that action at this time.