diff --git a/packages/core/src/linker/ng_module_factory_registration.ts b/packages/core/src/linker/ng_module_factory_registration.ts index b64442fbb0..866c6bd543 100644 --- a/packages/core/src/linker/ng_module_factory_registration.ts +++ b/packages/core/src/linker/ng_module_factory_registration.ts @@ -54,7 +54,7 @@ export function registerNgModuleType(ngModuleType: NgModuleType) { } } -export function clearModulesForTest(): void { +export function clearModuleRegistry(): void { modules.clear(); } diff --git a/packages/core/test/linker/ng_module_integration_spec.ts b/packages/core/test/linker/ng_module_integration_spec.ts index 5430ca3874..8f6038cee3 100644 --- a/packages/core/test/linker/ng_module_integration_spec.ts +++ b/packages/core/test/linker/ng_module_integration_spec.ts @@ -17,7 +17,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers'; import {modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing'; import {InternalNgModuleRef, NgModuleFactory} from '../../src/linker/ng_module_factory'; -import {clearModulesForTest} from '../../src/linker/ng_module_factory_registration'; +import {clearModuleRegistry} from '../../src/linker/ng_module_factory_registration'; import {stringify} from '../../src/util/stringify'; class Engine {} @@ -294,7 +294,11 @@ function declareTests(config?: {useJit: boolean}) { describe('id', () => { const token = 'myid'; - afterEach(() => clearModulesForTest()); + // Ivy TestBed clears module registry in resetTestingModule so this afterEach is not needed + // for Ivy + if (!ivyEnabled) { + afterEach(() => clearModuleRegistry()); + } it('should register loaded modules', () => { @NgModule({id: token}) diff --git a/packages/core/test/test_bed_spec.ts b/packages/core/test/test_bed_spec.ts index da6022635f..134b3fd1c6 100644 --- a/packages/core/test/test_bed_spec.ts +++ b/packages/core/test/test_bed_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Compiler, Component, Directive, ErrorHandler, Inject, Injectable, InjectionToken, Input, ModuleWithProviders, NgModule, Optional, Pipe, ɵsetClassMetadata as setClassMetadata, ɵɵdefineComponent as defineComponent, ɵɵdefineNgModule as defineNgModule, ɵɵtext as text} from '@angular/core'; +import {Compiler, Component, Directive, ErrorHandler, Inject, Injectable, InjectionToken, Input, ModuleWithProviders, NgModule, Optional, Pipe, getModuleFactory, ɵsetClassMetadata as setClassMetadata, ɵɵdefineComponent as defineComponent, ɵɵdefineNgModule as defineNgModule, ɵɵtext as text} from '@angular/core'; import {TestBed, getTestBed} from '@angular/core/testing/src/test_bed'; import {By} from '@angular/platform-browser'; import {expect} from '@angular/platform-browser/testing/src/matchers'; @@ -730,4 +730,17 @@ describe('TestBed', () => { .toEqual(originalResolver); }); }); + + onlyInIvy('Ivy module registration happens when NgModuleFactory is created') + .it('cleans up registered modules', async() => { + @NgModule({id: 'my_module'}) + class MyModule { + } + + expect(() => getModuleFactory('my_module')).toThrowError(); + await TestBed.inject(Compiler).compileModuleAsync(MyModule); + expect(() => getModuleFactory('my_module')).not.toThrowError(); + TestBed.resetTestingModule(); + expect(() => getModuleFactory('my_module')).toThrowError(); + }); }); diff --git a/packages/core/testing/src/r3_test_bed.ts b/packages/core/testing/src/r3_test_bed.ts index e754568142..44916613e3 100644 --- a/packages/core/testing/src/r3_test_bed.ts +++ b/packages/core/testing/src/r3_test_bed.ts @@ -35,6 +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'; let _nextRootElementId = 0; @@ -229,6 +230,7 @@ export class TestBedRender3 implements TestBed { } resetTestingModule(): void { + clearModuleRegistry(); this.checkGlobalCompilationFinished(); resetCompiledComponents(); if (this._compiler !== null) {