-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
TestBed.overrideProvider
(#16725)
This allows to overwrite all providers for a token, not matter where they were defined. This can be used to test JIT and AOT’ed code in the same way. Design doc: https://docs.google.com/document/d/1VmTkz0EbEVSWfEEWEvQ5sXyQXSCvtMOw4t7pKU-jOwc/edit?usp=sharing
- Loading branch information
Showing
20 changed files
with
735 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Injector} from '../di/injector'; | ||
import {NgModuleFactory, NgModuleRef} from '../linker/ng_module_factory'; | ||
import {Type} from '../type'; | ||
|
||
import {initServicesIfNeeded} from './services'; | ||
import {NgModuleDefinitionFactory, ProviderOverride, Services} from './types'; | ||
import {resolveDefinition} from './util'; | ||
|
||
export function overrideProvider(override: ProviderOverride) { | ||
initServicesIfNeeded(); | ||
return Services.overrideProvider(override); | ||
} | ||
|
||
export function clearProviderOverrides() { | ||
initServicesIfNeeded(); | ||
return Services.clearProviderOverrides(); | ||
} | ||
|
||
// Attention: this function is called as top level function. | ||
// Putting any logic in here will destroy closure tree shaking! | ||
export function createNgModuleFactory( | ||
ngModuleType: Type<any>, bootstrapComponents: Type<any>[], | ||
defFactory: NgModuleDefinitionFactory): NgModuleFactory<any> { | ||
return new NgModuleFactory_(ngModuleType, bootstrapComponents, defFactory); | ||
} | ||
|
||
class NgModuleFactory_ extends NgModuleFactory<any> { | ||
constructor( | ||
public readonly moduleType: Type<any>, private _bootstrapComponents: Type<any>[], | ||
private _ngModuleDefFactory: NgModuleDefinitionFactory) { | ||
// Attention: this ctor is called as top level function. | ||
// Putting any logic in here will destroy closure tree shaking! | ||
super(); | ||
} | ||
|
||
create(parentInjector: Injector|null): NgModuleRef<any> { | ||
initServicesIfNeeded(); | ||
const def = resolveDefinition(this._ngModuleDefFactory); | ||
return Services.createNgModuleRef( | ||
this.moduleType, parentInjector || Injector.NULL, this._bootstrapComponents, def); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.