One trick for 3 times faster `ng test`
Nov 25, 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
← Older post
What's new in Angular 11.0?
Nov 11, 2020
Newer post →
Angular Language Service with Ivy
Jan 19, 2021
Our books on sale
Become a ninja with Angular

Our free-price book about Angular, always up-to-date, in English and in French
Become a ninja with Vue

Our free-price book about Vue, always up-to-date, in English and in French
Next training sessions
- From Nov 17 to Nov 20, 2025Angular: Ninja to Hero (remote)
- From Dec 1 to Dec 4, 2025Vue: Zero to Ninja (remote)
- From Dec 8 to Dec 11, 2025Angular: Zero to Ninja (remote)
- From Jan 19 to Jan 22, 2026Angular: Ninja to Hero (remote)
- From Feb 9 to Feb 12, 2026Vue: Zero to Ninja (remote)
- From Mar 2 to Mar 5, 2026Angular: Zero to Ninja (remote)