I had the same problem recently. It was caused by a call to router.navigate
inside the ngOnInit
method of a component. The test was trying to create the component, but inside ngOnInit
it was attempting to navigate away from the component (because certain conditions were not met).
In my case, I am importing the RouterTestingModule
as part of TestBed.configureTestingModule
. So to fix this I simply registered a route with RouterTestingModule
. For example, suppose your navigation call looks like router.navigate(['example'])
and it resolves to ExampleComponent
. You can set up the test as follows:
RouterTestingModule.withRoutes([
{ path: 'example', component: ExampleComponent}
])
Doing the above allowed my tests to run without issuing Cannot match any routes
errors.
For what it’s worth, I think a better way would be to stub the router and just confirm that appropriate calls to navigate
are made.