Book Image

Jasmine Cookbook

By : Munish Kumar
Book Image

Jasmine Cookbook

By: Munish Kumar

Overview of this book

Table of Contents (16 chapters)
Jasmine Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

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.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Run spec file TDS_spec.js with the Jasmine runner (that is, SpecRunner.html)."

A block of code is set as follows:

describe("Employees of <XYZ> Company:",function(){
  //Scenario -1
  describe("Tax deducted for Indian Employees, ", function(){
    it("Currency should be used INR", function(){
    });  
  });
});

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

describe("Employees of <XYZ> Company:",function(){
  describe("Tax/TDS Currency", function(){
    //Scenario -1
    it("Currency should be used INR", function(){
      var myCurrency = new Currency("INDIA");
      expect(myCurrency.currency).toBe("INR");
    });  

Any command-line input or output is written as follows:

npm –-version
npm install karma-cli

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Here, notice that the error message is changed to Currency is undefined. Earlier, it was myCurrency is undefined."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

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 , 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 Errata Submission Form 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 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 , and we will do our best to address the problem.