In Angular, it is quite easy to break lazy-loading. For example, all it takes is for someone to carelessly import something from a lazy-loaded module into the app module and this module is eagerly loaded. Therefore, I usually check for such errors when reviewing PRs. Currently, I do this manually by looking at the logs of ng build
or by inspecting the network logs in the browser DevTools. However, I would like to automate this repetitive task in our CI pipeline.
After trying various methods, I can only think of two more or less suboptimal approaches:
- A script that builds the app and then checks if the expected number of lazy-loaded chunks is in the
dist
folder. However, this does not test when a chunk is loaded. - E2e tests that assert that a specific
.js
chunk file is loaded when the browser navigates to a specific route.
Is there any better way to programmatically check if lazy-loading works for all lazy-loaded modules?
EDIT: To be more precise: there are some answers on StackOverflow regarding this topic, but they usually rely so heavily on stubbing and mocking that, in the end, they don't test the actual implementation.