diff --git a/packages/router/testing/src/router_testing_harness.ts b/packages/router/testing/src/router_testing_harness.ts index 83aa74106703..2d4448568f6c 100644 --- a/packages/router/testing/src/router_testing_harness.ts +++ b/packages/router/testing/src/router_testing_harness.ts @@ -155,6 +155,10 @@ export class RouterTestingHarness { } return activatedComponent as T; } else { + if (requiredRoutedComponentType !== undefined) { + throw new Error(`Unexpected routed component type. Expected ${ + requiredRoutedComponentType.name} but the navigation did not activate any component.`); + } return null; } } diff --git a/packages/router/testing/test/router_testing_harness.spec.ts b/packages/router/testing/test/router_testing_harness.spec.ts index 9ee336fa38fb..48305cb6b75a 100644 --- a/packages/router/testing/test/router_testing_harness.spec.ts +++ b/packages/router/testing/test/router_testing_harness.spec.ts @@ -100,6 +100,20 @@ describe('navigateForTest', () => { await expectAsync(harness.navigateByUrl('/123', OtherCmp)).toBeRejected(); }); + it('throws an error if navigation fails but expected a component instance', async () => { + @Component({standalone: true, template: ''}) + class TestCmp { + } + + TestBed.configureTestingModule({ + providers: [ + provideRouter([{path: '**', canActivate: [() => false], component: TestCmp}]), + ] + }); + const harness = await RouterTestingHarness.create(); + await expectAsync(harness.navigateByUrl('/123', TestCmp)).toBeRejected(); + }); + it('waits for redirects using router.navigate', async () => { @Component({standalone: true, template: 'test'}) class TestCmp {