My question is similar to this question. I'm trying to write a test that checks the existence of a route in Angular. The main difference to the linked question, as that I'm using lazy loading modules instead of components.
I have a file app.routes.ts that contains the routes:
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: '',
loadChildren: () => import('./main/main.module').then(m => m.MainModule)
}
];
In the test file app.routes.spec.ts, I expect that this route exists:
import { routes } from './app.routes';
describe('app.routes', () => {
it('should contain a route for /', () => {
expect(routes).toContain({
path: '',
loadChildren: () => import('./main/main.module').then(m => m.MainModule)
});
});
});
When I run this, the test is not passing.
Expected [ Object({ path: '', loadChildren: Function }) ] to contain Object({ path: '', loadChildren: Function }). Error: Expected [ Object({ path: '', loadChildren: Function }) ] to contain Object({ path: '', loadChildren: Function }). at at UserContext. (http://localhost:9876/_karma_webpack_/src/app/app.routes.spec.ts:5:24) at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1) at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:305:1)
How can I fix this unit test, and make sure that the existence of lazy loaded routes can be tested? Thank you!
DISCLAIMER: I know that some people don't think this should be part of unit testing, and that routing should be tested in e2e testing. Personally I like the idea to check if all my routes exist, and there are no typos. The idea that if somebody would comment out a route for whatever reason and forgets to undo it, and automated testing catches this, makes me feel a bit safer.