@@ -29,7 +29,7 @@ import { RenderComponentOptions, RenderTemplateOptions, RenderResult } from './m
2929import { getConfig } from './config' ;
3030
3131const mountedFixtures = new Set < ComponentFixture < any > > ( ) ;
32- const inject = TestBed . inject || TestBed . get ;
32+ const safeInject = TestBed . inject || TestBed . get ;
3333
3434export async function render < ComponentType > (
3535 component : Type < ComponentType > ,
@@ -97,6 +97,17 @@ export async function render<SutType, WrapperType = SutType>(
9797
9898 const componentContainer = createComponentFixture ( sut , wrapper ) ;
9999
100+ const zone = safeInject ( NgZone ) ;
101+ const router = safeInject ( Router ) ;
102+
103+ if ( typeof router ?. initialNavigation === 'function' ) {
104+ if ( zone ) {
105+ zone . run ( ( ) => router ?. initialNavigation ( ) ) ;
106+ } else {
107+ router ?. initialNavigation ( ) ;
108+ }
109+ }
110+
100111 let fixture : ComponentFixture < SutType > ;
101112 let detectChanges : ( ) => void ;
102113
@@ -118,17 +129,6 @@ export async function render<SutType, WrapperType = SutType>(
118129 fixture . componentRef . injector . get ( ChangeDetectorRef ) . detectChanges ( ) ;
119130 } ;
120131
121- const zone = inject ( NgZone ) ;
122-
123- const router = inject ( Router ) ;
124- if ( typeof router ?. initialNavigation === 'function' ) {
125- if ( zone ) {
126- zone . run ( ( ) => router ?. initialNavigation ( ) ) ;
127- } else {
128- router ?. initialNavigation ( ) ;
129- }
130- }
131-
132132 const navigate = async ( elementOrPath : Element | string , basePath = '' ) : Promise < boolean > => {
133133 const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath . getAttribute ( 'href' ) ;
134134 const [ path , params ] = ( basePath + href ) . split ( '?' ) ;
@@ -227,7 +227,7 @@ export async function render<SutType, WrapperType = SutType>(
227227
228228async function createComponent < SutType > ( component : Type < SutType > ) : Promise < ComponentFixture < SutType > > {
229229 /* Make sure angular application is initialized before creating component */
230- await inject ( ApplicationInitStatus ) . donePromise ;
230+ await safeInject ( ApplicationInitStatus ) . donePromise ;
231231 return TestBed . createComponent ( component ) ;
232232}
233233
0 commit comments