DEV Community

How to Speed Up Angular Tests

John Au-Yeung on December 28, 2019

Subscribe to my email list now at http://jauyeung.net/subscribe/ Follow me on Twitter at https://twitter.com/AuMayeung Many more articles at http...
Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen • • Edited

The worst low performance issue occurs when components have external stylesheet and template files. For every test case, the compiler has to get access to, open and read two files per component, before it can even compile the component. So either use inline templates and styles or use the hack of preventing the Angular testing module from resetting between test cases.

Apparently, Ivy should make testing faster, but I haven't seen any facts about this yet.

If we just prevent the Angular testing module from resetting in beforeEach, we take care of the worst performance issue in tests and we don't have to add all declarables and dependencies to a single Angular testing module configuration which is hard to maintain and does not work well for all tests.

I used the hack with good results back in Angular version 5. I haven't tried it since then as I favor not using the TestBed at all for a lot of tests. One big caveat is that services and other dependencies are reused between test cases. So stateful service instances are not reset.

Collapse
 
joerter profile image
John Oerter • • Edited

Do you know if using some of the testing libraries such as angular-testing-library or spectator help with this issue? I haven't used either of them, but am beginning to experiment. Angular Testing Library at least looks like it may expose components in a different way that gets around this issue.

I too have stopped using the TestBed for most tests and simply test components by treating them as simple classes and asserting against their public properties, but am beginning to look at these other libraries as an easier and quicker way to test against the DOM

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen • • Edited

Both of them wrap TestBed so they will have exactly the same problems. We did discuss adding ng-bullet to Spectator, but decided to wait for Ivy.

Thread Thread
 
zaky7 profile image
Zakir •

I have seen even after Ivy, ng-bullet is still helps in speed up the tests. Is the discussion still open?

Thread Thread
 
layzee profile image
Lars Gyrup Brink Nielsen •

I'm not sure. You're welcome to file an issue to Spectator.

Collapse
 
aumayeung profile image
John Au-Yeung •

Pretty much every Angular component have these files.

Honestly, It's like the Angular team didn't test it at all with lots of components in a module.

Are there any alternatives to that doesn't involve adding recreating the module in every file?

It's not only the speed, but having to recreate the module code for every test file is also a pain.

Collapse
 
joerter profile image
John Oerter •

Interesting solution! I noticed several people mentioning ng-bullet in the issue you linked. Have you tried that? I'm curious to know more about your experiences.

Collapse
 
aumayeung profile image
John Au-Yeung •

I looked at Stack Overflow and this is the only way I found.

I haven't tried ng-bullet. Did people say it's good?

Collapse
 
joerter profile image
John Oerter •

Yes, it seemed to solve a lot of people's issues.

Thread Thread
 
aumayeung profile image
John Au-Yeung •

I have to check it out.

Thanks for finding that.

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen •

Details on what takes up the most time in Angular tests:
gist.github.com/Quramy/1dd5bed0bce...

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen •
Collapse
 
adisreyaj profile image
Adithya Sreyaj •

Interesting...

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen • • Edited

"Next-level testing in Angular Ivy version 9" by yours truly
indepth.dev/next-level-testing-in-...

Collapse
 
aumayeung profile image
John Au-Yeung •

Thanks. That's a useful find.