fix(ivy): Only restore registered modules if user compiles modules with TestBed (#32944)

There are a couple scenarios that are problematic and need special
handling:

1. A user has a custom implementation of lazy-loaded modules, sets some
provider overrides, then compiles the module so it can be loaded. In a
follow-up test, the user sets different overrides for the module and
then compiles. This is problematic because we need to be sure the module
registered in the first test is not used, so we need to clear it out of
the modules list in `ng_module_factory_registration`.
2. A user has a similar lazy-loaded module factory implementation but
relies on the module being registered automatically. This can happen,
for example, as a side effect of importing the ngfactory file.

PR Close #32944
This commit is contained in:
Andrew Scott
2019-10-01 15:02:59 -07:00
committed by atscott
parent 01e4d44e8c
commit 63256b511a
5 changed files with 49 additions and 19 deletions

View File

@ -35,7 +35,7 @@ import {MetadataOverride} from './metadata_override';
import {TestBed} from './test_bed';
import {ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, TestBedStatic, TestComponentRenderer, TestModuleMetadata} from './test_bed_common';
import {R3TestBedCompiler} from './r3_test_bed_compiler';
import {clearModuleRegistry} from '../../src/linker/ng_module_factory_registration';
import {clearRegisteredModuleState} from '../../src/linker/ng_module_factory_registration';
let _nextRootElementId = 0;
@ -230,7 +230,6 @@ export class TestBedRender3 implements TestBed {
}
resetTestingModule(): void {
clearModuleRegistry();
this.checkGlobalCompilationFinished();
resetCompiledComponents();
if (this._compiler !== null) {

View File

@ -8,6 +8,7 @@
import {ResourceLoader} from '@angular/compiler';
import {ApplicationInitStatus, COMPILER_OPTIONS, Compiler, Component, Directive, Injector, LOCALE_ID, ModuleWithComponentFactories, ModuleWithProviders, NgModule, NgModuleFactory, NgZone, Pipe, PlatformRef, Provider, Type, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵDirectiveDef as DirectiveDef, ɵNG_COMPONENT_DEF as NG_COMPONENT_DEF, ɵNG_DIRECTIVE_DEF as NG_DIRECTIVE_DEF, ɵNG_INJECTOR_DEF as NG_INJECTOR_DEF, ɵNG_MODULE_DEF as NG_MODULE_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵNgModuleFactory as R3NgModuleFactory, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵNgModuleType as NgModuleType, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵgetInjectableDef as getInjectableDef, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵsetLocaleId as setLocaleId, ɵtransitiveScopesFor as transitiveScopesFor, ɵɵInjectableDef as InjectableDef} from '@angular/core';
import {ModuleRegistrationMap, getRegisteredModulesState, restoreRegisteredModulesState} from '../../src/linker/ng_module_factory_registration';
import {clearResolutionOfComponentResourcesQueue, isComponentDefPendingResolution, resolveComponentResources, restoreComponentResolutionQueue} from '../../src/metadata/resource_loading';
@ -41,6 +42,7 @@ interface CleanupOperation {
export class R3TestBedCompiler {
private originalComponentResolutionQueue: Map<Type<any>, Component>|null = null;
private originalRegisteredModules: null|ModuleRegistrationMap = null;
// Testing module configuration
private declarations: Type<any>[] = [];
@ -264,6 +266,9 @@ export class R3TestBedCompiler {
* @internal
*/
async _compileNgModuleAsync(moduleType: Type<any>): Promise<void> {
if (this.originalRegisteredModules === null) {
this.originalRegisteredModules = getRegisteredModulesState();
}
this.queueTypesFromModulesArray([moduleType]);
await this.compileComponents();
this.applyProviderOverrides();
@ -535,6 +540,10 @@ export class R3TestBedCompiler {
this.initialNgDefs.clear();
this.moduleProvidersOverridden.clear();
this.restoreComponentResolutionQueue();
if (this.originalRegisteredModules) {
restoreRegisteredModulesState(this.originalRegisteredModules);
this.originalRegisteredModules = null;
}
// Restore the locale ID to the default value, this shouldn't be necessary but we never know
setLocaleId(DEFAULT_LOCALE_ID);
}