Book Image

Jasmine JavaScript Testing Update

By : Paulo Vitor Zacharias Ragonha
Book Image

Jasmine JavaScript Testing Update

By: Paulo Vitor Zacharias Ragonha

Overview of this book

Table of Contents (15 chapters)


This book is about being a better JavaScript developer. So, throughout the chapters, you will not only learn about writing tests in the Jasmine 'idiom', but also about the best practices in writing software in the JavaScript language. It is about acknowledging JavaScript as a real platform for application development and leveraging all its potential. It is also about tooling and automation and how to make your life easier and more productive.

Most importantly, this book is about craftsmanship of not only working software, but also well-crafted software.

Jasmine JavaScript Testing, Second Edition is a practical guide to writing and automating JavaScript testing for web applications. It uses technologies such as Jasmine, Node.js, and webpack.

Over the course of the chapters, the concept of test-driven development is explained through the development of a simple stock market Investment Tracker application. It starts with the basics of testing through the development of the base domain classes (such as stock and investment), passes through the concepts of maintainable browser code, and concludes with a full refactoring to a React.js application build on ECMA Script 6 modules and automated build.

What this book covers

Chapter 1, Getting Started with Jasmine, covers the motivations behind testing a JavaScript application. It presents the concept of BDD and how it helps you to write better tests. It also demonstrates how easy it is to download Jasmine and start coding your first tests.

Chapter 2, Your First Spec, helps you learn the thought process behind thinking in terms of test-driven development. You will code your very first JavaScript functionality driven by tests. You will also learn the basic functions of Jasmine and how to structure your tests. Also demonstrated, is how Jasmine matchers work and how you can create one of your own to improve your tests' code readability.

Chapter 3, Testing Frontend Code, covers some patterns in writing maintainable browser code. You will learn about thinking in terms of components and how to use the module pattern to better organize your source files. You will also be presented with the concept of HTML fixtures and how you can use it to test your JavaScript code without requiring your servers to render an HTML. You will also learn about a Jasmine plugin called jasmine-jquery and how it can help you write better tests with jQuery.

Chapter 4, Asynchronous Testing – AJAX, talks about the challenges in testing AJAX requests and how you can use Jasmine to test any asynchronous code. You will learn about Node.js and how to create a very simple HTTP server to use as a fixture to your tests.

Chapter 5, Jasmine Spies, presents the concept of test doubles and how to use spies to do behavior checking.

Chapter 6, Light Speed Unit Testing, helps you to learn about the issues with AJAX testing and how you can make your tests run faster using stubs or fakes.

Chapter 7, Testing React Applications, introduces you to React, a library to build user interfaces, and covers how you can use it to improve the concepts presented in Chapter 3, Testing Frontend Code, to create richer and more maintainable applications, of course, driven by tests.

Chapter 8, Build Automation, presents you with the power of automation. It introduces you to webpack, a bundling tool for frontend assets. You will start to think in terms of modules and their dependencies, and you will learn how to code your tests as modules. You will also learn about packing and minifying the code to production and how to automate this process. Finally, you are going to learn about running your tests from a command line and how this can be used in a continuous integration environment with

What you need for this book

Besides a browser and a text editor, the only requirement to run some of the examples, is Node.js 0.10.x.

Who this book is for

This book is a must-have material for web developers new to the concept of unit testing. It's assumed that you have a basic knowledge of JavaScript and HTML.


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: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

describe("Investment", function() {
  it("should be of a stock", 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("Investment", function() {
  it("should be of a stock", function() {

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

# npm install --save-dev webpack

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: "Clicking the Next button moves you to the next screen."


Warnings or important notes appear in a box like this.


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

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 for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit 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, 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 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 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 , and we will do our best to address the problem.