39,59 €
If you are a competent JavaScript developer who wants to design and implement tests using Jasmine in order to minimize bugs in the production environment, then this book is ideal for you. Some familiarity with unit testing and code coverage concepts such as branch coverage along with basic knowledge of Node.js, AngularJS, and CoffeeScript is required.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 260
Veröffentlichungsjahr: 2015
Copyright © 2014 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2015
Production reference: 1200415
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-716-6
www.packtpub.com
Author
Munish Sethi
Reviewer
Devin Weaver
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Larissa Pinto
Content Development Editor
Ajinkya Paranjape
Technical Editor
Vivek Pala
Copy Editors
Pranjali Chury
Kriti Sharma
Project Coordinator
Harshal Ved
Proofreaders
Martin Diver
Elinor Perry-Smith
Paul Hindle
Indexer
Mariammal Chettiyar
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
Munish Sethi is a postgraduate in computer science and MBA in IT from SCDL. He has 18 years of industry experience and has a sound knowledge of test automation, test- and behavior-driven development, white box testing, big data testing, review techniques, test estimation, and requirement analysis. Currently, he is working with Impetus Infotech Pvt. Ltd. where he plays the role of test craftsman and automation architect. With his diverse experience, he helps engineering teams with deliveries and also with designing test solutions and formulating test strategies for his clients. He has also designed and implemented many automation frameworks (such as modular, hybrid, data-driven, keyword-driven, and page-object models) using Jasmine/Protractor, QTP, Selenium Web driver, Junit, TestNG, SOAPUI, Sikuli, Test Complete, and so on. He's very keen to explore new and upcoming tools and technologies.
He has worked with both product- and services-based organizations. Munish's Twitter handle is @munishksethi.
First of all, I would like to thank Lord Krishna for inspiring me at every movement. You have given me the power to believe in my passion and pursue my dreams. I could have never done this without the faith I have in you, the Almighty.
I would like to take this opportunity to thank the team at Packt Publishing for all of their outstanding work in helping to bring this book to fruition. They were very supportive and their processes are designed to improve the quality of the book. I have thoroughly enjoyed working with them throughout the entire process, and appreciate their candid feedback, which ultimately raised the standard of my work. I would like to give a big thank you to the reviewer, Devin Weaver, whose constructive criticisms and observations have also served to raise the standard of the final product.
I would also like to say thanks to Rakesh Gupta, AVP of Engineering, Impetus, for his continuous support and for encouraging me to write this book.
Finally, I would like to say thanks to my wife, Sweeni Sethi, and my kids, Pranjali and Punya, for their never-ending support, which keeps me going.
I would like to thank my mother, Sudesh Sethi, and father, Jagdish Lal Sethi, for giving me constant morale throughout my life.
Devin Weaver is a software developer by day and a software hobbyist and mentor by night. His interest in programming started with his first Commodore 64, where he learned BASIC from the manual. Since then he has studied many languages, and JavaScript is among his favorites. He is dedicated towards his family. He continues to learn and strives to achieve a level of quality above the rest.
I would like to thank my life partner, Maddy Pendleton-Weaver, for her unwavering confidence in me, and her love. I thank my mother, Marcia Ball, for her continued support and encouragement of my interests.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Today, application quality is a major concern among organizations. Lack of quality not only affects one's credibility but is also detrimental for end users and business operations. It is very important to develop applications by applying best practices and effective processes, and exercising the entire code base before deploying it on the production.
Jasmine is an open source Behavior-Driven Development (BDD) framework for testing JavaScript applications. It encourages good testing practices and application development using a BDD approach. Jasmine provides a rich set of libraries to design and develop tests for JavaScript (or JavaScript-enabled platform) applications. The entire application code can be exercised to ensure the quality and correctness of functional behavior. Jasmine also provides the mechanism to define custom functions and spies to test end-to-end (E2E) scenarios.
This book is written to get you up and running with Jasmine faster than any other book. Each chapter focuses on a specific aspect of Jasmine and is broken down into easy-to-follow recipes. While most chapters make use of previous skills that you'll have learnt from earlier the text, you don't necessarily need to read each chapter in order. Each recipe begins with the explicit steps that you need to accomplish the goal of the recipe, followed by an explanation as well as some other ideas you may wish to explore on your own.
Lastly, let's focus on building high-quality apps with Jasmine to minimize defect leakage in production. All you need is this book, a computer with Jasmine, and a constructive mind.
Chapter 1, Getting Started with Jasmine Framework, introduces the basics of Jasmine, demonstrates its usage, and explains how unit tests will be designed for JavaScript. This also discusses the role of matchers for writing Jasmine specs.
Chapter 2, Jasmine with TDD and BDD Processes, describes how Jasmine tests will be designed along with application code using TDD and BDD processes, and also explains how to write Jasmine specs for new and existing code by applying TDD and BDD processes.
Chapter 3, Customizing Matchers and Jasmine Functions, elaborates Jasmine functions and the need of custom matchers and their usage. This also examines the concept of pending specs and designing Jasmine specs for exceptions.
Chapter 4, Designing Specs from Requirement, explains how meaningful specs can be designed by determining test requirements.
Chapter 5, Jasmine Spies, illustrates the concept of mocking and how to create Jasmine tests for methods or objects which are dependent on other methods or objects. This also demonstrates how to develop custom or user-defined mock functions.
Chapter 6, Jasmine with AJAX, jQuery, and Fixtures, elaborates how to design and develop a Jasmine spec for AJAX calls, jQuery, and asynchronous operations. This also examines the data-driven approach and how to design Jasmine specs with fixtures to manipulate DOM.
Chapter 7, Code Coverage with Jasmine Tests, covers how to configure code coverage tools and generate coverage reports using karma and istanbul, and it also explores how to increase code coverage for the existing or legacy code.
Chapter 8, Jasmine with Other Tools, describes how to implement Jasmine specs for AngularJS, CoffeeScript, and Node.js.
Chapter 9, Developing JavaScript Apps Using Jasmine - A Real-time Scenario, elaborates a real-time scenario to develop JavaScript applications using Jasmine and explains how to design specs with HTML using BDD approach, and also examines how to develop specs for a Change Request and validate the output of a method, object, or unit using the data-driven approach.
Jasmine should be pre-installed on your computer before starting with Jasmine Cookbook. We'll be working with Jasmine 2.x. You can download the Jasmine from https://github.com/pivotal/jasmine/releases. We will be using Notepad++ or WebStorm/NetBeans IDE to design the Jasmine specs and the Firefox, Chrome, or Internet Explorer browser to run the specs. You will need to have access to a reasonably modern computer, with a reasonably modern operating system. For Windows users, Windows XP, 7 or newer should do fine. I'd recommend at least 2 gigabytes of RAM. Mac OS X and Linux users should also be OK, as long they have a similar or superior amount of RAM.
If you are a competent JavaScript developer who wants to design and implement tests using Jasmine in order to minimize bugs in the production environment, then this book is ideal for you. Some familiarity with unit testing and code coverage concepts such as branch coverage along with basic knowledge of Node.js, AngularJS, and CoffeeScript is required.
In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the ErrataSubmissionForm link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
In this chapter, we will cover:
Nowadays, JavaScript has become the de facto programming language to build and empower frontend/web applications. We can use JavaScript to develop simple or complex applications. However, applications in production are often vulnerable to bugs caused by design inconsistencies, logical implementation errors, and similar issues. For this reason, it is usually difficult to predict how applications will behave in real-time environments, which leads to unexpected behavior, non-availability of applications, or outages for short or long durations. This generates lack of confidence and dissatisfaction among application users. Also, high cost is often associated with fixing the production bugs. Therefore, there is a need to develop applications that are of a high quality and that offer high availability.
Jasmine plays a vital role to establish effective development process by applying efficient testing processes. Jasmine is an excellent framework for testing JavaScript code both in browser and on the server side. We well use version 2.0.1 of Jasmine in this book. In this chapter, we will cover how tests can be written for JavaScript code using the Jasmine framework. You will also learn how various Jasmine matchers play a pivotal role for writing the tests.