📎 Webclip
How to Speed Up Angular Tests
Angular tests are slow by default because many dependencies are injected and the tested components are reloaded for each test. The article says this can make runs last 30 minutes or more, so the fix is to load the dependencies once per test file and reuse them.
Reading notes#
- Create a shared
spec-helper.spec.tsfile and define abeforeTestfunction there. - Inside
beforeAll, reset the testing module, configureTestBedwith the app components, modules, and providers, and compile the components once. - After compilation, override
TestBed.resetTestingModuleso Angular does not reset the testing module again. - Import that helper in spec files and call
beforeTest. - This setup makes unit tests run much faster, from about 30 minutes to a few seconds.
- It also reduces the need to repeat dependencies in every test file and lowers the risk of missing a dependency in a test module.
