question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

SharedStylesHost isn't cleaning up styles between Karma test specs

See original GitHub issue

๐Ÿž Bug Report

Affected package

Latest

Is this a regression?

No

Description

Wheen testing components via TestBed.createComponent, Angular appends inline styles associated with the component via the SharedStylesHost service. These inline styles are cleaned up when the service is destroyed.

Unfortunately, these inline styles are never cleaned up in Karma unit tests. This appears to be because each SharedStylesHost service is never destroyed, apparently because each associated NgModule is never destroyed between specs. This isnโ€™t a problem when you have <10 Karma specs, but when you have >100 , and each spec appends 25 <style> tags, by the 100th spec you have 2500 redundant <style> tags in your document head.

๐Ÿ”ฌ Minimal Reproduction

https://stackblitz.com/edit/angular-testing-exxbrh (old version of angular, but repros on latest)

๐ŸŒ Your Environment

Angular Version:

Floating version of Angular 2 RC branch. Repros on latest.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:23
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
cexbrayatcommented, Nov 23, 2020

Until the fix lands, you can add to test.ts:

afterEach(() => {
  getTestBed().inject(ษตDomSharedStylesHost).ngOnDestroy();
});

Unlike the workaround mentioned above https://github.com/angular/angular/issues/31834#issuecomment-591366744, this only cleans the necessary stylesheets and not the global ones.

This speeds up our ng test task by 2-3x (from 120s to 40s on my machine) without breaking anything.

6reactions
elesueurcommented, Mar 11, 2021

@AndrewKushnir whatโ€™s blocking the MR in 38336 and resolution of this issue?

This issue is pretty significant and for large apps could have a MAJOR impact on unit test execution. An issue like this opened in July 2019 and still not resolved boggles my mind.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Speed up Angular unit test duration - Luminis
These inline styles are cleaned up when the service is destroyed. Unfortunately, these inline styles are never cleaned up in Karma unit tests....
Read more >
Angular unit tests are leaking styles - jasmine - Stack Overflow
Does anybody know how to make karma/angular to cleanup after each test? I am using angular 6.1.1 and angular/cli 6.0.1. angular ยท jasmine....
Read more >
Jasmine & Karma โ€ข Angular - codecraft.tv
Jasmine is a testing framework that supports Behavior-Driven Development. We write tests in Test Suites which are composed of one or more Test...
Read more >
Angular ใฎ Unit Test ใง style ใŒๅข—ใˆ็ถšใ‘ใ‚‹ๅ•้กŒใ‚’ๅ›ž้ฟใ™ใ‚‹ใจ
่ฟฝ่จ˜ 2021/12/3 : Angular v13 ใ‹ใ‚‰ใฏใ€ใ“ใฎๅ•้กŒใฏ็™บ็”Ÿใ—ใชใ„ใ‚ˆใ†ใงใ™: ่ฉณ็ดฐ SharedStylesHost isn't cleaning up styles between Karma test specs ...
Read more >
Test Angular Components with Jasmine, Karma, and the Test ...
Test Wrapper Component. When you run the generate command, you'll get a boilerplate spec file to build tests. The file is set up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found