1
- import { Provider , Type } from '@angular/core' ;
1
+ import { Type } from '@angular/core' ;
2
2
import { TestBed , waitForAsync } from '@angular/core/testing' ;
3
3
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing' ;
4
4
@@ -7,6 +7,7 @@ import { setProps } from '../internals/query';
7
7
import * as customMatchers from '../matchers' ;
8
8
import { addMatchers } from '../core' ;
9
9
import { isType } from '../types' ;
10
+ import { ModuleMetadata } from '../base/initial-module' ;
10
11
11
12
import { initialSpectatorModule } from './initial-module' ;
12
13
import { getSpectatorDefaultOptions , SpectatorOptions } from './options' ;
@@ -73,30 +74,19 @@ export function createComponentFactory<C>(typeOrOptions: Type<C> | SpectatorOpti
73
74
74
75
const moduleMetadata = initialSpectatorModule < C > ( options ) ;
75
76
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
+ } ) ) ;
92
80
93
81
return ( overrides ?: SpectatorOverrides < C > ) => {
94
82
const defaults : SpectatorOverrides < C > = { props : { } , detectChanges : true , providers : [ ] } ;
95
83
const { detectChanges, props, providers } = { ...defaults , ...overrides } ;
96
84
97
85
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
100
90
} ) ;
101
91
}
102
92
@@ -118,3 +108,28 @@ function createSpectator<C>(options: Required<SpectatorOptions<C>>, props?: Part
118
108
119
109
return new Spectator ( fixture , debugElement , component , debugElement . nativeElement ) ;
120
110
}
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