Jasmine Cookbook - Munish Sethi - E-Book

Jasmine Cookbook E-Book

Munish Sethi

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Key Features

    Book Description

    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.

    What you will learn

    • Develop JavaScript applications with Jasmine using the behaviordriven development (BDD) process
    • Apply custom matchers by enhancing your Jasmine tests to test specific features or functionality
    • Design Jasmine specs for jQuery and Ajax with HTML and JSON fixtures
    • Generate code coverage analysis for JavaScript code using Karma and Istanbul
    • Create Jasmine tests for CoffeeScript and AngularJS
    • Implement E2E (endtoend) Jasmine specs for JavaScript applications to simulate a realtime scenario

    Who this book is for

    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:

    EPUB
    MOBI

    Seitenzahl: 260

    Veröffentlichungsjahr: 2015

    Bewertungen
    0,0
    0
    0
    0
    0
    0
    Mehr Informationen
    Mehr Informationen
    Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



    Table of Contents

    Jasmine Cookbook
    Credits
    About the Author
    About the Reviewer
    www.PacktPub.com
    Support files, eBooks, discount offers, and more
    Why Subscribe?
    Free Access for Packt account holders
    Preface
    What this book covers
    What you need for this book
    Who this book is for
    Sections
    Getting ready
    How to do it…
    How it works…
    There's more…
    See also
    Conventions
    Reader feedback
    Customer support
    Downloading the example code
    Errata
    Piracy
    Questions
    1. Getting Started with Jasmine Framework
    Introduction
    Writing your first Jasmine test
    Getting ready
    How to do it...
    How it works...
    See also
    Adding specs to your Jasmine test
    How to do it…
    How it works...
    See also
    Adding expectations and matchers to the test
    How to do it…
    How it works...
    Applying different matchers to the Jasmine test
    How to do it…
    How it works...
    Applying setup and teardown functions to the Jasmine test
    Getting ready
    How to do it…
    How it works...
    Using the "this" keyword
    Getting ready
    How to do it…
    How it works...
    2. Jasmine with TDD and BDD Processes
    Introduction
    Writing Jasmine tests using TDD and BDD processes
    Getting ready
    How to do it...
    How it works...
    Creating a Jasmine test for new code using TDD and BDD
    Getting ready
    How to do it...
    How it works...
    Designing Jasmine tests for existing code using TDD and BDD
    Getting ready
    How to do it...
    How it works...
    Defining nested suites to write more meaningful specs
    Getting ready
    How to do it...
    How it works...
    3. Customizing Matchers and Jasmine Functions
    Introduction
    Writing a Jasmine test using a custom equality tester function
    How to do it...
    How it works...
    Writing Jasmine tests with custom matchers
    How to do it...
    How it works...
    Writing Jasmine tests for exceptions
    How to do it...
    How it works...
    Declaring pending specs with Jasmine tests
    Getting ready
    How to do it...
    4. Designing Specs from Requirement
    Introduction
    Writing useful specs by analyzing the test requirements
    How to do it...
    How it works...
    Implementing the Jasmine test corresponding to the specs
    How to do it...
    How it works...
    Applying Acceptance Criteria to the Jasmine test
    How to do it...
    How it works...
    Organizing Jasmine specs into groups and subgroups
    How to do it...
    5. Jasmine Spies
    Introduction
    Writing Jasmine tests using the spyOn() method and special matchers for spies
    Getting ready
    How to do it...
    How it works...
    Writing Jasmine tests using tracking properties
    How to do it…
    How it works...
    Writing Jasmine tests with your own custom spy method
    Getting ready
    How to do it...
    How it works...
    Using jasmine.any and jasmine.objectContaining
    Getting ready
    How to do it…
    How it works...
    Writing Jasmine tests to mock the JavaScript timeout functions
    How to do it…
    How it works...
    6. Jasmine with AJAX, jQuery, and Fixtures
    Introduction
    Writing Jasmine specs for AJAX
    Getting ready
    How to do it...
    How it works...
    Designing Jasmine specs with HTML and JSON fixtures
    Getting ready
    How to do it...
    How it works...
    Writing Jasmine specs for jQuery
    Getting ready
    How to do it...
    How it works...
    Writing Jasmine specs using custom jQuery matchers
    Getting ready
    How to do it…
    How it works...
    Writing Jasmine specs for asynchronous operations using the done() function
    How to do it…
    How it works...
    7. Code Coverage with Jasmine Tests
    Introduction
    Configuring code coverage tools
    Getting ready
    How to do it...
    How it works...
    See also
    Generating code coverage using karma and istanbul
    Getting ready
    How to do it...
    How it works...
    See also
    Writing Jasmine specs for uncovered branches/code
    How to do it...
    How it works...
    Increasing code coverage for existing code
    How to do it...
    How it works...
    8. Jasmine with Other Tools
    Introduction
    Writing Jasmine tests for AngularJS
    Getting ready
    How to do it...
    How it works...
    Using Jasmine with CoffeeScript
    Getting ready
    How to do it...
    How it works...
    Writing Jasmine tests for Node.js
    Getting ready
    How to do it...
    How it works...
    9. Developing JavaScript Apps Using Jasmine – a Real-time Scenario
    Introduction
    Defining Jasmine specs corresponding to a user story
    Getting ready
    How to do it...
    See also
    Implementing Jasmine specs with Web/HTML
    Getting ready
    How to do it...
    How it works...
    Implementing Jasmine tests with a Data-Driven approach
    How to do it...
    Creating Jasmine tests for a change request
    Getting ready
    How to do it...
    Index

    Jasmine Cookbook

    Jasmine Cookbook

    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

    Credits

    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

    About the Author

    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.

    About the Reviewer

    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.

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    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.

    Why Subscribe?

    Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

    Free Access for Packt account holders

    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.

    Preface

    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.

    What this book covers

    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.

    What you need for this book

    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.

    Who this book is for

    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.

    Sections

    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:

    Getting ready

    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.

    How to do it…

    This section contains the steps required to follow the recipe.

    How it works…

    This section usually consists of a detailed explanation of what happened in the previous section.

    There's more…

    This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

    See also

    This section provides helpful links to other useful information for the recipe.

    Reader feedback

    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.

    Customer support

    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.

    Downloading the example code

    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.

    Errata

    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

    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.

    Questions

    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.

    Chapter 1. Getting Started with Jasmine Framework

    In this chapter, we will cover:

    Writing your first Jasmine testAdding specs to your Jasmine testAdding expectations and matchers to the testApplying different matchers to the Jasmine testApplying setup and teardown functions to the Jasmine testUsing the "this" keyword

    Introduction

    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.