fix(ivy): avoid using stale cache in TestBed if module overrides are defined (#33787)

NgModule compilation in JIT mode (that is also used in TestBed) caches module scopes on NgModule defs (using `transitiveCompileScopes` field). Module overrides (defined via TestBed.overrideModule) may invalidate this data by adding/removing items in `declarations` list. This commit forces TestBed to recalculate transitive scopes in case module overrides are present, so TestBed always gets the most up-to-date information.

PR Close #33787
This commit is contained in:
Andrew Kushnir
2019-11-12 23:03:46 -08:00
committed by Alex Rickabaugh
parent a09d60cb50
commit fbd2133610
3 changed files with 83 additions and 16 deletions

View File

@ -420,19 +420,25 @@ export function patchComponentDefWithScope<C>(
/**
* Compute the pair of transitive scopes (compilation scope and exported scope) for a given module.
*
* This operation is memoized and the result is cached on the module's definition. It can be called
* on modules with components that have not fully compiled yet, but the result should not be used
* until they have.
* By default this operation is memoized and the result is cached on the module's definition. You
* can avoid memoization and previously stored results (if available) by providing the second
* argument with the `true` value (forcing transitive scopes recalculation).
*
* This function can be called on modules with components that have not fully compiled yet, but the
* result should not be used until they have.
*
* @param moduleType module that transitive scope should be calculated for.
* @param forceRecalc flag that indicates whether previously calculated and memoized values should
* be ignored and transitive scope to be fully recalculated.
*/
export function transitiveScopesFor<T>(
moduleType: Type<T>,
processNgModuleFn?: (ngModule: NgModuleType) => void): NgModuleTransitiveScopes {
moduleType: Type<T>, forceRecalc: boolean = false): NgModuleTransitiveScopes {
if (!isNgModule(moduleType)) {
throw new Error(`${moduleType.name} does not have a module def (ɵmod property)`);
}
const def = getNgModuleDef(moduleType) !;
if (def.transitiveCompileScopes !== null) {
if (!forceRecalc && def.transitiveCompileScopes !== null) {
return def.transitiveCompileScopes;
}
@ -471,13 +477,9 @@ export function transitiveScopesFor<T>(
throw new Error(`Importing ${importedType.name} which does not have a ɵmod property`);
}
if (processNgModuleFn) {
processNgModuleFn(importedType as NgModuleType);
}
// When this module imports another, the imported module's exported directives and pipes are
// added to the compilation scope of this module.
const importedScope = transitiveScopesFor(importedType, processNgModuleFn);
const importedScope = transitiveScopesFor(importedType, forceRecalc);
importedScope.exported.directives.forEach(entry => scopes.compilation.directives.add(entry));
importedScope.exported.pipes.forEach(entry => scopes.compilation.pipes.add(entry));
});
@ -496,7 +498,7 @@ export function transitiveScopesFor<T>(
if (isNgModule(exportedType)) {
// When this module exports another, the exported module's exported directives and pipes are
// added to both the compilation and exported scopes of this module.
const exportedScope = transitiveScopesFor(exportedType, processNgModuleFn);
const exportedScope = transitiveScopesFor(exportedType, forceRecalc);
exportedScope.exported.directives.forEach(entry => {
scopes.compilation.directives.add(entry);
scopes.exported.directives.add(entry);
@ -512,7 +514,9 @@ export function transitiveScopesFor<T>(
}
});
def.transitiveCompileScopes = scopes;
if (!forceRecalc) {
def.transitiveCompileScopes = scopes;
}
return scopes;
}