fix(ivy): R3TestBed should clean up registered modules after each test (#32872)

PR Close #32872
This commit is contained in:
Andrew Scott 2019-09-26 17:03:29 -07:00 committed by atscott
parent 53b32f17b3
commit 475e36abb5
4 changed files with 23 additions and 4 deletions

View File

@ -54,7 +54,7 @@ export function registerNgModuleType(ngModuleType: NgModuleType) {
} }
} }
export function clearModulesForTest(): void { export function clearModuleRegistry(): void {
modules.clear(); modules.clear();
} }

View File

@ -17,7 +17,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
import {modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing'; import {modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing';
import {InternalNgModuleRef, NgModuleFactory} from '../../src/linker/ng_module_factory'; 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'; import {stringify} from '../../src/util/stringify';
class Engine {} class Engine {}
@ -294,7 +294,11 @@ function declareTests(config?: {useJit: boolean}) {
describe('id', () => { describe('id', () => {
const token = 'myid'; 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', () => { it('should register loaded modules', () => {
@NgModule({id: token}) @NgModule({id: token})

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * 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 {TestBed, getTestBed} from '@angular/core/testing/src/test_bed';
import {By} from '@angular/platform-browser'; import {By} from '@angular/platform-browser';
import {expect} from '@angular/platform-browser/testing/src/matchers'; import {expect} from '@angular/platform-browser/testing/src/matchers';
@ -730,4 +730,17 @@ describe('TestBed', () => {
.toEqual(originalResolver); .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();
});
}); });

View File

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