Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Jasmine Cookbook
Jasmine Cookbook

Jasmine Cookbook: Over 35 recipes to design and develop Jasmine tests to produce world-class JavaScript applications

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Jasmine Cookbook

Chapter 2. Jasmine with TDD and BDD Processes

In this chapter, we will cover:

  • Writing Jasmine tests using TDD and BDD processes
  • Creating a Jasmine test for new code using TDD and BDD
  • Designing Jasmine tests for existing code using TDD and BDD
  • Defining nested suites to write more meaningful specs

Introduction

Behavior-Driven Development (BDD) is a software development process based on Test-Driven Development (TDD). Before we start writing Jasmine tests using TDD and BDD, let's get a briefing of how both processes work and how they play a pivotal role in software development.

TDD is a unit testing based test-first or fail-first approach where we write the test before writing the code. Thereafter, we write the minimum amount of code just to pass the test. Finally, we refactor the code to acceptable standards. This form of approach focuses on the specification of the application, thus decreasing the probability of bugs. In other words, we can say that TDD is a defect-prevention approach that focuses on internal (code) quality.

A JavaScript application can be developed using TDD by performing the following steps:

  1. Write a failing unit test.
  2. Make the test pass.
  3. Refactor the test.
  4. Repeat the steps.
  5. When you cannot think of any more tests or there is no more scope for refactoring, you...

Writing Jasmine tests using TDD and BDD processes

To write a Jasmine test using TDD and BDD, let's assume that we are developing a payroll application for <XYZ> company. The employees of <XYZ> company work in India, UK, and the US. As per immediate need, a Tax (that is, Tax Deduction at Source) module needs to be developed to deduct an employee's tax in salary disbursement. There are different business rules that exist for deducting tax across different countries.

"As payroll administrator, I want to calculate an employee's tax on salary disbursement so that I can evaluate the exact tax."

Let's consider some scenarios in the context that an employee's tax should be deducted on salary disbursement:

  • Scenario-1: Tax should be deducted in currency INR (India Rupee, Rs) for Indian employees
  • Scenario-2: Tax should be deducted in currency GBP (British Pound, UK £) for UK employees
  • Scenario-3: Tax should be deducted in currency USD (US Dollar, $) for...

Creating a Jasmine test for new code using TDD and BDD

Writing Jasmine tests for new code is straightforward compared to writing Jasmine tests for existing code. In this recipe, we will learn how to design specs and write tests for new code.

To write a Jasmine test for new code using TDD and BDD, let's assume that you are working with a bank in a credit card division. There are different business rules that exist to accept credit card numbers.

"As a credit card administrator, I want to validate credit card numbers so that I can check their authenticity for further processing."

Let's consider a scenario in the following context that a credit card number should be valid to shop in online store:

Scenario-1: The credit card number should have 16 digits for the "Master Card" category

Getting ready

Let's define the Scenario in the Given/When/Then format as follows:

  • Given: Credit card division of a bank
  • When: Accepted credit card number for Master Card category
  • Then...

Designing Jasmine tests for existing code using TDD and BDD

Writing Jasmine tests for existing code is tricky compared to writing Jasmine tests for new code. Consider that you are updating the existing code against a change request. Then, there can be the following situations where you write Jasmine tests with the existing code:

  • The existing code is developed without using TDD and BDD processes and no test is written for the existing code.
  • The existing code is developed without using TDD and BDD processes. However, Jasmine tests are developed for the existing code.
  • The existing code is developed using TDD and BDD processes and Jasmine tests are already written for the existing code.

Whatever the scenario is valid for you from the above list, but when we implement a new feature or functionality, it’s recommended to develop Jasmine tests only for the new piece of code. Also, if Jasmine tests are not written for the existing code, then we need to develop the tests only for the code that...

Defining nested suites to write more meaningful specs

Sometimes, we need to refactor Jasmine specs by defining a suite or suites within a suite (that is, nested suites) to make it more granular and readable. This helps all stakeholders to understand the functionality from a holistic perspective. Moreover, you can easily identify and map the failing spec(s) with an exact piece of code. In this recipe, you will learn to design specs with nested suites.

Note

In Jasmine, we can define suites using the describe global function. This function accepts two parameters (string and function()). The string parameter is for naming the collection of specs and function() is a block of code that implements the suite.

Getting ready

You will learn this recipe with the help of a previous recipe in this chapter. For more information, refer to the Designing the Jasmine test for existing code using TDD and BDD recipe.

To understand when and how to define nested test suites to write more meaningful specs, let&apos...

Left arrow icon Right arrow icon

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.

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 24, 2015
Length: 276 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393281
Vendor :
Pivotal
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Apr 24, 2015
Length: 276 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393281
Vendor :
Pivotal
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 104.97
Jasmine Cookbook
€41.99
Jasmine JavaScript Testing Update
€29.99
Test-Driven JavaScript Development
€32.99
Total 104.97 Stars icon
Banner background image

Table of Contents

10 Chapters
1. Getting Started with Jasmine Framework Chevron down icon Chevron up icon
2. Jasmine with TDD and BDD Processes Chevron down icon Chevron up icon
3. Customizing Matchers and Jasmine Functions Chevron down icon Chevron up icon
4. Designing Specs from Requirement Chevron down icon Chevron up icon
5. Jasmine Spies Chevron down icon Chevron up icon
6. Jasmine with AJAX, jQuery, and Fixtures Chevron down icon Chevron up icon
7. Code Coverage with Jasmine Tests Chevron down icon Chevron up icon
8. Jasmine with Other Tools Chevron down icon Chevron up icon
9. Developing JavaScript Apps Using Jasmine – a Real-time Scenario Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(7 Ratings)
5 star 42.9%
4 star 42.9%
3 star 14.3%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Rene Lopez| Jun 04, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book covering the most of the common scenarios that you will find for test Javascript Apps in General.I actually was looking for a book that provided a concise and curated content for making tests for Javascript Apps and for me this is it, as it gives you kind of real business scenarios as examples for developing tests.We are always try to avoid testing and focus most on the time developing things but thanks to this book, you can follow a more TDD practices on my code and give more quality to it. A must reference for any JS Developer.
Amazon Verified review Amazon
Dipti Jun 15, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Book is designed to help both novice and advance users to understand and implement.It uses TDD and BDD process for the implementation, which makes implementation and execution of my project quite easy for new and existing code. It even has Code Coverage analysis explained using Karma and Istanbul tools. This book gives us a fair idea about the usage of Jasmine Spies from E2E perspective, Jasmine tests with jQuery, Asynchronous operations, Implementation of Fixtures and manipulation of DOM with Jasmine tests. It has vast variety of examples provided on Data driven approach, Angular JS, Node.js and CoffeeScript with Jasmine tests.Examples given in this book are to the point, overall a good book to understand and implement Jasmine tests following all good practices.
Amazon Verified review Amazon
Arun Mahendrakar Sep 11, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I've read other books on Jasmine, but this one clearly has been the best. It starts out real basic defining of the Jasmine jargon-ology (I guess I just coined a new word). The first three chapters are for those who want to start out with Jasmine for the first time. TDD/BDD are described in detail in the early chapters, which helps a newbie to level-set his frame of mind on how to think in terms of writing code and unit tests.Usage of advanced concepts like working with spies, mocking AJAX, jQuery and Fixtures are divulged pretty well. The author recognizes the importance of code-coverage and dedicates an entire chapter to discuss it.As expected, the author does mention how to unit test code written using AngularJS and CoffeeScript code. The wide-range scope of Jasmine demonstrates the maturity and versatility of the testing framework.I'm hoping a future version of this book will contain a section on build automation, but this is a really great book for both beginners and experienced JavaScript developers.
Amazon Verified review Amazon
Vipul Gupta Jun 11, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I have been involved in doing evaluation of a lot of tools and frameworks to suite the day to day project and client needs. Recently did a research on "Jasmine" to validate and check it's implementation feasibility for BDD when I hit "Jasmine Cookbook".I was quite intrigued when I received this book written by Munish Sethi, as it provides practical ways how a novice can actually learn Jasmine quickly and easily. So, is it a ideal reference book for a product team to get handson on Jasmine quickly?In short: Yes. It acts as a great reference for the teams who want to implement Jasmine for their products. More importantly it acts as a quick reference for anyone who wants to begin with Jasmine.This book is divided into 9 logical chapters with each chapter focusing on a particular need that any team might be having at any particular phase of a SDLC i.e. from evaluation phase till actual implementation and measurement phase. Some of the key focus areas that the writer has brought out very clearly are1.How Jasmine can be implemented in teams following either TDD or BDD. It becomes easy for the user to understand and implement it in the projects thereafter.2.All examples relate to the real world scenarios a layman might get into, hence, makes them easy to understand3.Provides a detailed step by step approach to write your own custom "equality" and "matcher" functions in Jasmine4.Performing mocking using spyOn() method, Asynchronous operations and Implementation of Fixtures and manipulation of DOM with Jasmine tests is explained in detail.5.Includes practical usage and designing of Jasmine based automated tests to validate complex functionality developed using AJAX, jQuery, JSON Fixtures6.Apart from the automated tests, it also includes methodologies to validate the code coverage achieved through the automated tests using JavaScript Code Coverage tool Karma and Istanbul that can enable product teams to keep a check on what they are testing.Towards the end, Jasmine integration and usage with other tools like Angular JS, Node.js and CoffeeScript is touched upon. Though these could have been detailed further, but it gives a platform to quickly start in case there is a need for these technologies. But, overall the book is a great guide for a product team to take small steps to learn and implement Jasmine as per their needs.
Amazon Verified review Amazon
Dwarika Jul 16, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book has been best design for experienced developer of JavaScript, Jquery and Ajax but presentation of content is more like a beginners guide that seems biggest USP of this book.In this book, First Three chapter are just for beginners at some extent because this section is going to help you to understand setting up Jasmine and would also teach you how to design unit tests for JavaScript and would also give you overview about matchers those gonna help you to design Jasmine Specs for pending specs along with exceptions. After going through these three chapter i am pretty sure that you would be able to differentiate the need of TDD and BDD approach . and you will get an analytical thought about the use of TDD and BDD in your project. So if you are a beginner then be happy because these three chapters are written for you and after that you would be able to understand the use of this Jasmine framework.My favorite chapter is seventh chapter that talks more about Code Coverage with Jasmine Tests, then question arises why do i like this chapter So my answer is , here you are going to generate the coverage report that is going to keep your business people satisfied and intend them to think ' all is going well <It more like slogan of 3 Idiots movie 'All iz well'>' because this shows all you effort in numbers and business people do believe in numbers and percentage. This can be achieved only after going through this chapter and from here you would be able to understand the basics of code coverage that is used in our development or automation test creation. So believe me this is the chapter that is going to bring more appreciation from your business people and senior people in company hierarchy for you. Don't worry, All the numbers and percentage of your code coverage would be taken care by the use of karma and istanbul tools. Best thing that is in this chapter is its 'How to do it' section because this is just a step by step implementation in real time project.Apart from all above what I like most is, its running code that can be either created by you while practicing this book or the same can be cloned from git repository. So after going through this book, I am pretty sure you would be pretty clear about Jasmine framework and its better use in real time environment.So at the end of this book you would be able to see one bonus chapter that is going to help you to create JavaScript App using Jasmine so this is the chapter that has been written for the pro -developers who have already master the Jasmine framework.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.