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 a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781784397166
Vendor :
Pivotal
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Apr 24, 2015
Length: 276 pages
Edition : 1st
Language : English
ISBN-13 : 9781784397166
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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.