I’m currently looking into optimising a whole bunch of Angular (16) unit tests but cannot find a good and consistent guide on doing so. I’ll list out some important factors and then thing’s I’ve attempted to do to optimise tests. If I’m missing anything please let me know.
- A vast majority of our tests are targetting services, or component logic, not the DOM and templates.
- We don’t use afterEach() anywhere, from what I’ve seen this is mainly useful for clearing the JSDOM manually after executing the test case but I don’t think this is really relevant to us.
- We have about 330 test suites, and 1400 test cases, our code coverage isn’t as high as it should be but we’re fine with this.
- On Component.spec.ts files we just initialise the component, mock all component dependencies and then just have a simple ‘it should create’
- In attempts to find memory leaks and other issues we’ve utilised ‘–detect-open-handles’ and ‘–logHeapUsage’ whilst also resolving any console warnings/errors.
- We try to use Mocks of dependencies instead of having the test creating an actual instance of all dependencies
I haven’t really shown an example of our code because I’m looking more for config settings or ways of cleaning up depending on the context.