diff --git a/packages/playwright-ct-angular/registerSource.mjs b/packages/playwright-ct-angular/registerSource.mjs index 22115cf7b0..2b6bbf856f 100644 --- a/packages/playwright-ct-angular/registerSource.mjs +++ b/packages/playwright-ct-angular/registerSource.mjs @@ -28,7 +28,6 @@ import { BrowserDynamicTestingModule, platformBrowserDynamicTesting, } from '@angular/platform-browser-dynamic/testing'; -import { Router } from '@angular/router'; getTestBed().initTestEnvironment( BrowserDynamicTestingModule, @@ -106,9 +105,6 @@ async function __pwRenderComponent(component) { __pwUpdateSlots(WrapperComponent, component.slots, componentMetadata.selector); - // TODO: only inject when router is provided - TestBed.inject(Router).initialNavigation(); - const fixture = TestBed.createComponent(WrapperComponent); fixture.nativeElement.id = 'root'; diff --git a/tests/components/ct-angular/playwright/index.ts b/tests/components/ct-angular/playwright/index.ts index eea37a2089..573635a367 100644 --- a/tests/components/ct-angular/playwright/index.ts +++ b/tests/components/ct-angular/playwright/index.ts @@ -1,10 +1,11 @@ import '@angular/compiler'; import { beforeMount, afterMount } from '@playwright/experimental-ct-angular/hooks'; -import { provideRouter } from '@angular/router'; +import { Router, provideRouter } from '@angular/router'; import { ButtonComponent } from '@/components/button.component'; import { TOKEN } from '@/components/inject.component'; import { routes } from '@/router'; import '@/assets/styles.css'; +import { APP_INITIALIZER, inject } from '@angular/core'; export type HooksConfig = { routing?: boolean; @@ -18,7 +19,17 @@ beforeMount(async ({ hooksConfig, TestBed }) => { if (hooksConfig?.routing) TestBed.configureTestingModule({ - providers: [provideRouter(routes)], + providers: [ + provideRouter(routes), + { + provide: APP_INITIALIZER, + multi: true, + useFactory() { + const router = inject(Router); + return () => router.initialNavigation(); + } + } + ], }); if (hooksConfig?.injectToken)