One trick for 3 times faster `ng test`
25 nov. 2020
I know, the title sounds like a click bait. But it's not, I assure you.
There is an issue in the Angular framework that leads to a memory leak in unit tests,
which slows down the browser executing the tests, and results in looong ng test
.
The issue has been around for quite some time, and will be fixed in the framework in a future version (maybe 11.x or 12?).
But in the meantime, this workaround can lead to 3 times faster tests. On the project I first tested it, 1631 tests went from taking 2min50s to 50s on CI (from 2min to 40s on my laptop) 🚀
TL:DR;
In a CLI project, open test.ts
and add the following lines:
import { ɵDomSharedStylesHost } from '@angular/platform-browser';
// https://github.com/angular/angular/issues/31834
afterEach(() => {
getTestBed().inject(ɵDomSharedStylesHost).ngOnDestroy();
});
And let us know how much faster is your ng test
!
Why?
When we test a component, the framework inserts its styles in
← Article plus ancien
What's new in Angular 11.0?
11 nov. 2020
Article plus récent →
Angular Language Service with Ivy
19 janv. 2021
Étiquettes
Nos livres en vente
Deviens un ninja avec Angular

Notre livre sur Angular à prix libre, maintenu à jour, en français et en anglais
Deviens un ninja avec Vue

Notre livre sur Vue à prix libre, maintenu à jour, en français et en anglais
Prochaines sessions de formation
- Du 6 au 9 oct. 2025Angular : de Zéro à Ninja (à distance)
- Du 17 au 20 nov. 2025Angular : de Ninja à Héros (à distance)
- Du 1 au 4 déc. 2025Vue : de Zéro à Ninja (à distance)
- Du 8 au 11 déc. 2025Angular : de Zéro à Ninja (à distance)
- Du 19 au 22 janv. 2026Angular : de Ninja à Héros (à distance)
- Du 9 au 12 févr. 2026Vue : de Zéro à Ninja (à distance)
- Du 2 au 5 mars 2026Angular : de Zéro à Ninja (à distance)