1- import { Provider , Type } from '@angular/core' ;
1+ import { Type } from '@angular/core' ;
22import { TestBed , waitForAsync } from '@angular/core/testing' ;
33import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing' ;
44
@@ -7,6 +7,7 @@ import { setProps } from '../internals/query';
77import * as customMatchers from '../matchers' ;
88import { addMatchers } from '../core' ;
99import { isType } from '../types' ;
10+ import { ModuleMetadata } from '../base/initial-module' ;
1011
1112import { initialSpectatorModule } from './initial-module' ;
1213import { getSpectatorDefaultOptions , SpectatorOptions } from './options' ;
@@ -73,30 +74,19 @@ export function createComponentFactory<C>(typeOrOptions: Type<C> | SpectatorOpti
7374
7475 const moduleMetadata = initialSpectatorModule < C > ( options ) ;
7576
76- beforeEach (
77- waitForAsync ( ( ) => {
78- addMatchers ( customMatchers ) ;
79- TestBed . configureTestingModule ( moduleMetadata ) . overrideModule ( BrowserDynamicTestingModule , {
80- set : {
81- entryComponents : moduleMetadata . entryComponents
82- }
83- } ) ;
84-
85- overrideModules ( options ) ;
86-
87- overrideComponentIfProviderOverridesSpecified ( options ) ;
88-
89- TestBed . compileComponents ( ) ;
90- } )
91- ) ;
77+ beforeEach ( waitForAsync ( ( ) => {
78+ configureAndCompileTestingModule ( moduleMetadata , options ) ;
79+ } ) ) ;
9280
9381 return ( overrides ?: SpectatorOverrides < C > ) => {
9482 const defaults : SpectatorOverrides < C > = { props : { } , detectChanges : true , providers : [ ] } ;
9583 const { detectChanges, props, providers } = { ...defaults , ...overrides } ;
9684
9785 if ( providers && providers . length ) {
98- providers . forEach ( ( provider : Provider ) => {
99- TestBed . overrideProvider ( ( provider as any ) . provide , provider as any ) ;
86+ TestBed . resetTestingModule ( ) ;
87+ initializeTestingModule ( {
88+ ...options ,
89+ providers
10090 } ) ;
10191 }
10292
@@ -118,3 +108,28 @@ function createSpectator<C>(options: Required<SpectatorOptions<C>>, props?: Part
118108
119109 return new Spectator ( fixture , debugElement , component , debugElement . nativeElement ) ;
120110}
111+
112+ function initializeTestingModule < C > ( typeOrOptions : Type < C > | SpectatorOptions < C > ) : void {
113+ const options = isType ( typeOrOptions )
114+ ? getSpectatorDefaultOptions < C > ( { component : typeOrOptions } )
115+ : getSpectatorDefaultOptions ( typeOrOptions ) ;
116+
117+ const moduleMetadata = initialSpectatorModule < C > ( options ) ;
118+
119+ configureAndCompileTestingModule ( moduleMetadata , options ) ;
120+ }
121+
122+ function configureAndCompileTestingModule < C > ( moduleMetadata : ModuleMetadata , options : Required < SpectatorOptions < C > > ) : void {
123+ addMatchers ( customMatchers ) ;
124+ TestBed . configureTestingModule ( moduleMetadata ) . overrideModule ( BrowserDynamicTestingModule , {
125+ set : {
126+ entryComponents : moduleMetadata . entryComponents
127+ }
128+ } ) ;
129+
130+ overrideModules ( options ) ;
131+
132+ overrideComponentIfProviderOverridesSpecified ( options ) ;
133+
134+ TestBed . compileComponents ( ) ;
135+ }
0 commit comments