Each day we develop more and more web apps where the source code is 80% JavaScript. The Server side component becomes a mere data service, usually a simple RESTful service. The amount of classes and logic in the Client side is getting larger, and more complex each time, and evidently they are more likely to fail with each update of the code.
To decrease the possibility of failures in our applications there is a paradigm called Test-driven development, based on unit tests.
I believe that, nowadays, all developers have heard something about Unit Testing (UT). If this is your first time then you could start by reading some of the following links:
It is very likely that you are among those who have used Unit Tests while developing in the server side, but I’d bet you have never used Unit Tests in the Client side of your applications, specifically in Javascript and AngularJS.
Right?
If so, don’t worry, here I'll try to introduce you to this fascinating world.
The tools for UT are widespread in the Server side, either in .Net or Java you can find solutions. IDEs like Visual Studio, Eclipse and others, integrate perfectly with the Unit Testing tools and facilitate the creation and execution of Unit Tests.
But what’s happening on the other side of the fence?
This is a question that I‘ve been wondering for a long time. I confess I tried to read about this a couple of years ago and I finished a little bit disappointed. At least where I read, the generalized comments were about projects that have been abandoned, and I couldn’t find anything else interesting enough.
I think these projects were not receiving the required attention, perhaps for a cost problem, perhaps because almost everybody was doing well without using Unit Testing on Javascript, or simply perhaps because still the predominating applications had more than 70% of their source code in the Server side.
But personally, I am having a strong feeling that the development cycle is incomplete, it's like having a table with only three legs.
You can argue that so far, the table is working fine with only three legs...
And yes, it's true, but nobody will deny that supporting the table with four legs will give it more support and will make it stronger.
Observing the development of Angular during the last year I have begun to note that many of their code's examples are making use of unit testing. I went again over the subject and I'm greatly surprised.
The Unit Testing tools for Javascript and Angular have evolved very much, they are as flexible and powerful as their counterparts in the Server side. Even more, there are managers of projects and dependencies for the Client side with the same style of Maven and Gradle for the Server side.
Some of the main tools are:
However, we can create unit tests without any of the tools mentioned above. We can use one of the more evolved frameworks nowadays: Jasmine.
Jasmine is a powerful and flexible framework for unit tests in Javascript with as much or more flexibility than any of the ones that we use on the Server side. It has a very simple and intuitive syntax.
I can remember at least two or three recent projects where I’ve had to build a sort of "hand made" page to test the Angular components, either services or controllers. Well, not anymore, this is past history.
We also have the ngMock angular module that offers support for all the tasks related to unit tests on Angular's components.
There is a "seed" project in GitHub, ready to be used, including many of these tools pre-configured. In all honesty I think that, despite the facilities offered by this project, it’s too heavy and might not fit your development environment.
I actually prefer to use a pure and clean version of Jasmine. I was looking in GitHub for a cleaner boilerplate project but I haven't really found one containing exactly what I want, or maybe I got tired of looking. So, I have created a basic project containing only the required Jasmine's libraries and a couple of examples with Unit Test for Javascript and Angular. https://github.com/deisbel/angular-jasmine-standalone-seed
What are the most important elements of Jasmine that we need to use? - Only a few:
- A copy of the jasmine-standalone libraries.
This translates to just download the libraries from the Jasmine website and add them to the project's root folder.
- Create unit tests.
This is just writing tests using the Jasmine "language" and saving them in a file with extension .js. You can save these files mixed among others belonging to your app's source code or in a separate folder in the project's root folder.
- Add a simple html page in charge of running the tests, by default named SpecRunner.html.
Requesting the page from any browser is all you need to do to see the results. This page should be updated to add a reference to each test file that you have created and you want to run.
- If you also want to test components of your application defined in Angular (such as Services, Filters, and Controllers) you also must add references to angular and angular-mock libraries.
To show how simple is to write a file to create a test we will use the following Person class, serving us as target for our unit tests.
We are going to create three simple tests to verify that the Person's properties are always properly instantiated, and also, to ensure that the full name of the person is correctly created. These tests are pretty simple, but good enough for our purpose: illustrating how easy can be the process.
How should we define our SpecRunner.html file to execute all of these test?
It should look like this:
So far, I've shown you the less complex cases, only for a pure Javascript class, but how can I create unit tests for my Angular app components?
It's enough by adding a couple more references to our SpecRunner.html page, one for angular.js and another for angular-mocks.js.
Also, you can see that we added two more references, one for the file where an Angular controller is defined and another for the file where the unit tests for that controller are created. The content for each file is below:
If you refresh the SpecRunner.html page to execute again the tests you will see these results:
You can add more files containing classes and tests as you need them.
After reading this article you don’t have any more excuses to ignore unit testing in the Client side, and we can finally add the fourth leg to our table, achieving more reliable apps, happier users and clients.
To deepen in the topic I give you the main links:
- Unit testing in AngularJS: https://docs.angularjs.org/guide/unit-testing
To download the project and collaborations:
https://github.com/deisbel/angular-jasmine-standalone-seed
https://github.com/deisbel/angular-jasmine-standalone-seed
Collecting the relevant things and everyone want to go through it.
ReplyDeleteSoftware Testing Course in Madurai
Software Testing Training in Madurai
Software Testing Madurai
Software Testing Course in Coimbatore
Software Testing Training in Coimbatore
Software Testing Course in Coimbatore with Placement
This was an well written blog and I am happy reading your blog.
ReplyDeleteSpoken English Class in Thiruvanmiyur
Spoken English Classes in Adyar
Spoken English Classes in T-Nagar
Spoken English Classes in Vadapalani
Spoken English Classes in Porur
Spoken English Classes in Anna Nagar
Spoken English Classes in Chennai Anna Nagar
Spoken English Classes in Perambur
Spoken English Classes in Anna Nagar West
Excellent blog!!! I got to know the more useful information by reading your blog. Thanks for posting this blog.
ReplyDeleteEnglish Speaking Classes in Mulund
English Speaking Classes in Mulund West
English Speaking Course in Mulund
English Speaking Course in Mulund East
English Speaking Course in Mulund West
Best English Speaking Classes in Mulund West
English Speaking Classes Mulund
Wow,nice information you posted here. Keep up the excellent work.
ReplyDeleteManual Testing Training in Chennai
Manual Testing Training
Manual Testing Course
Manual Testing Training in Porur
Mobile Testing Training in Chennai
mobile testing course in chennai
Drupal Training in Chennai
Photoshop Classes in Chennai
ReplyDeleteNice blog!! I hope you will share more info like this. I will use this for my studies and research.
Tally Course in Chennai
Tally Classes in Chennai
Angularjs Training in Chennai
CCNA Training in Chennai
Salesforce Training in Chennai
Tally Training in Chennai
The article is so informative. This is more helpful for our
ReplyDeletebest software testing training in chennai
best software testing training institute in chennai with placement
software testing training
courses
software testing training and placement
software testing training online
software testing class
software testing classes in chennai
best software testing courses in chennai
automation testing courses in chennai
Thanks for sharing.
ReplyDeleteYou write this post very carefully I think, which is easily understand to me. Not only this, other post is also good. As a newbie this info is really helpful for me. Thanks to you.
tally training
Tally Training in Chennai
Tally ERP 9 Training
Tally Course
tally classes
Tally institute in Chennai
Tally Training institute in Chennai
Tally course in Chennai
ReplyDeleteThis content of information has
helped me a lot. It is very well explained and easy to understand.
seo training in chennai
seo training classes
seo training course
you use a fantastic weblog here! if you’d like to cook some invite posts in my blog? website design la
ReplyDeleteAll in all I see Horrible Bosses, you could do a lot worst, if you’re one of the people who the films’ content appeals to see it, it would be a safe choice. los angeles web agency
ReplyDelete
ReplyDeleteGreat post. Thanks for sharing.....
Java Training in Bangalore
Java Classes in Pune
Java Training in Hyderabad
Java Training in Delhi
Java Training in Gurgaon
Amazing Information! keep updating your Blog like this.
ReplyDeleteTally Institute in Chennai
Tally Training in Chennai
Great post. keep sharing such a worthy information
ReplyDeleteCloud Computing Training in Chennai
Cloud Training in Chennai
Amazing post... Thanks for sharing...
ReplyDelete.Net training in chennai
.net coaching centre in chennai
Great post. keep sharing such a worthy information
ReplyDeleteDigital Marketing Course in Chennai
Best digital marketing course online
Digital Marketing Courses in Bangalore
Great post. keep sharing such a worthy information
ReplyDeleteEthical Hacking Course in Chennai
Ethical Hacking course in Bangalore
Nice information.. keep updating
ReplyDeleteRPA Training in Chennai
RPA course in Chennai
RPA Training Institute in Chennai
RPA Training in Bangalore
Robotic Process Automation Training in Bangalore
RPA Classes in Bangalore
Fabulous blog keep updating like this.....
ReplyDeleteBest DevOps Training in Chennai
DevOps Training institute in Chennai
DevOps Course in Bangalore
DevOps Training Institutes in Bangalore
instagram takipçi satın al
ReplyDeleteinstagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
Great post. Thanks for sharing such a useful blog.
ReplyDeleteSuccessful graphic designer
Graphic Designer
Really nice blog. thanks for sharing
best selenium training in chennai
Best selenium Training Institute in Chennai
This post is so interactive and informative.keep update more information...
ReplyDeleteHadoop Training in Anna Nagar
Big data training in chennai
Mindblowing blog very useful thanks
ReplyDeleteHadoop Training in T Nagar
Big Data Training in T Nagar
En son çıkan perde modelleri
ReplyDeletesms onay
Vodafone Mobil Ödeme Bozdurma
nft nasıl alınır
Ankara evden eve nakliyat
Trafik Sigortasi
dedektör
KURMA WEBSİTESİ
aşk kitapları
smm panel
ReplyDeletesmm panel
iş ilanları blog
instagram takipçi satın al
hirdavatciburada.com
https://www.beyazesyateknikservisi.com.tr/
servis
Jeton Hile İndir
Axure RP Pro 10.0.0.3882 Crack 2022 is a rapid prototyping tool in addition to providing wireframing, and specification to align development and documentation .Axure RP Pro 7.0 Full Crack
ReplyDeleteGood content. You write beautiful things.
ReplyDeletehacklink
hacklink
mrbahis
vbet
sportsbet
mrbahis
korsan taksi
sportsbet
taksi
salt likit
ReplyDeletesalt likit
T8CRCS
Thanks for the informative blog.
ReplyDeletealso,check Java Classes in Nagpur