DEV Community

An Angular Testing Cheatsheet

Esteban HernΓ‘ndez on October 22, 2018

Personal note This is a redacted version of an internal document I prepared for a client. It is based off the most recent revision and i...
Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen

Thanks, Esteban!

Too few resources on Angular testing even though testability is a core concern for the Angular framework.

Collapse
 
sourvinos profile image
John Sourvinos

The state of Angular Testing as of today is: Non-exinstent documentation, obsolete books that focus around the big problem if 1+1 equals 2, incomplete tutorials and posts by "professionals" who in their spare time are fathers, tech enthusiasts and pet lovers, youtubers advertising themselves as experts and mvp's that the only thing they achive is to break the 100 subscriber barrier, non-native english speakers with horrible accent who record their so called 'tutorials' by using their phone's microphone and/or with incomprehensible accents. Books are written and advertised as professional series, and they leave testing for the very end, instead of being at the very beginning after the creation of the new project. Of course, they only cover the basics such as checking if a component has been created or if a fake service returns the expected results. That's it. A trained cat can check if an h1 tag contains 'Hello world'. Not a single attempt to go deeper, as if the subject must be avoided like the plague. At the end of each day, I wonder whether to burn the .spec.ts files and do everything manually with dubious results, spend endless hours of researching in sources here and there, or to look somewhere else, maybe React, even though I have my doubts that the situation over there is any better. And above all I also have 'experts' telling me not to test the private methods. Well, it's my f***ing projects and I will test whatever I want.

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen

I would offer you a listing of my articles on testing Angular applications, but I'm afraid that I'm a father, a tech enthusiast, and a Microsoft MVP, so you might not be interested πŸ˜„

Thread Thread
 
sourvinos profile image
John Sourvinos

Yourself and Net Basal are the only ones I have discovered so far to be of any interest. As a continuation to my response, since I'm fussy, picky and perfectionist, I'm looking for the 0.5% of knowledge which the 99.5% don't seem to care about - this is the reason for my initial reply and I dedicate it to the vast majority. So, throw me anything you've got! Hope I'm making sence! Greetings from 39Β°38'N / 19Β°55'E.

Thread Thread
 
layzee profile image
Lars Gyrup Brink Nielsen • Edited
Thread Thread
 
sourvinos profile image
John Sourvinos

Lars,

Super awesome, thanks a million!

Have a great weekend!
Regards,
John

Collapse
 
mapteb_54 profile image
Nalla Senthilnathan

John, recently I have shared a technique that might reduce frustrations like you have expressed
dev.to/mapteb/angular-testing-a-si...

Collapse
 
sergiorom92 profile image
sergiorom92

Thanks a lot!!

Nice article! Helped me a lot!! Testing definitely makes software better, this is a great resource. Every software developer must understant its importance, it's one of best practices. Greetings from Colombia.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
lysofdev profile image
Esteban HernΓ‘ndez

Hey, try separating your large function into smaller ones that can be tested in isolation.

If you have several simple pipes that depend on each other, try combining them in a custom pipe and test that with a mock observable.

So for RxJS, you can:

  • Test a simple function by using mocked inputs.
  • Test a custom pipe by using a mocked observable.
  • Test a custom observable by mocking the sources or events.

My general rule of thumb is that a function should hardly ever have more than two or three tests. When a function requires more than that, you can probably break it down into two or more.

Collapse
 
vinaymcscet profile image
vinay kumar

I need to help on writing testcase for this functions.

private element: any;
@Input() id: string;
constructor(private el: ElementRef) {
this.element = el.nativeElement;
}
ngOnInit(): void {
if (!this.id) {
return;
}
this.element.addEventListener("click", (el: any) => {
if (el.target.className === "tfb-modal-background") {
this.close();
}
});
}

Collapse
 
vinaymcscet profile image
vinay kumar

I need to help on writing testcase for this functions.
private element: any;
@Input() id: string;

constructor(private el: ElementRef) {
this.element = el.nativeElement;
}

ngOnInit(): void {
if (!this.id) {
return;
}
this.element.addEventListener("click", (el: any) => {
if (el.target.className === "tfb-modal-background") {
this.close();
}
});
}

Collapse
 
chan_austria777 profile image
chan πŸ€–

Nice article but i hope this gets updated with the latest api

Collapse
 
robbiesmith79 profile image
Robbie Smith

You have a typo in one of your examples: changeCouner

Collapse
 
aerojeyenth profile image
Jeyenthaaran N

Thanks for this very nice article on testing!
Is it possible to use Jasmine Marbles to test services instead of mocking it using a spy?

Collapse
 
deepachaurasia1 profile image
Deepa Chaurasia

Hey what if input variable is coming from another component where we have retrieved it from backend api service.

Do we still follow same approach as mentioned by you