Book Image

Learning Behavior-driven development with Javascript

Book Image

Learning Behavior-driven development with Javascript

Overview of this book

Table of Contents (17 chapters)
Learning Behavior-driven Development with JavaScript
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

JavaScript is not only widely used to create attractive user interfaces for the Web, but, with the advent of Node.js, it is also becoming a very popular and powerful language with which to write server-side applications. In this context, JavaScript systems are no longer toy applications, and their complexity has grown exponentially. To create complex applications that behave correctly, it is almost mandatory to cover these systems with an automated test suite. This is especially true in JavaScript because it does not have a compiler to help developers. Unfortunately, it is easy to fall into testing pitfalls that will make your test suite brittle; hard to maintain, and sooner or later, they will become another headache instead of a solution. Using behavior-driven development and some common testing patterns and best practices, you will be able to avoid these traps.

A lot of people see the whole TDD/BDD approach as a black-and-white decision. Either you do not do it, or you try to achieve a hundred percent test coverage. The real world calls for a more pragmatic approach: write the tests that really pay off and do not write those that do not give you much value. To be able to take this kind of decision, a good knowledge of BDD and the costs associated with it is needed.

What this book covers

Chapter 1, Welcome to BDD, presents the basic concepts that act as a foundation for BDD. Its goal is to debunk a few false preconceptions about BDD and to clarify its nomenclature. It is the only theoretical chapter in the whole book.

Chapter 2, Automating Tests with Mocha, Chai, and Sinon, introduces the basic tools for testing in JavaScript. We will go through the installation process and some simple examples of testing. You can safely skip this chapter if you are well versed with these tools.

Chapter 3, Writing BDD Features, presents some techniques for transforming a functional requirement written in normal language into a set of automated BDD tests or features. We will write our first BDD feature.

Chapter 4, Cucumber.js and Gherkin, repeats the exercise of the previous chapter but this time using Cucumber.js. This way we can compare it fairly with Mocha. You can safely skip this chapter if you already know Cucumber.js.

Chapter 5, Testing a REST Web API, shows you how to test not only the core logic, but also the Node.js server that publishes a Web API. This chapter will be of special interest if you are writing a REST API.

Chapter 6, Testing a UI Using WebDriverJS, shows you how to approach testing the UI layer from the perspective of BDD. You will also learn about WebDriverJS and how it can help you in this task.

Chapter 7, The Page Object Pattern, explains how to create robust UI tests that are less susceptible to being broken by UI design changes. For that, we will apply the page object pattern.

Chapter 8, Testing in Several Browsers with Protractor and WebDriver, shows you how to use the Protractor framework to run your test suite in several browsers.

Chapter 9, Testing Against External Systems, gives you some basic techniques for doing this and, most important, shows you when not to do it. Although this kind of test is not strictly BDD, sometimes you do need to test against external systems.

Chapter 10, Final Thoughts, briefly summarizes the book and clarifies the right granularity for BDD testing. It will also tell you whether to do only BDD at the core business level, or add additional tests at other levels.

What you need for this book

You can follow the code samples in this book using any modern PC or laptop. The code samples should work on Linux and OS X. You can follow the code using Windows, too, but keep in mind that you will need to slightly modify the command-line commands shown in the book to the Windows syntax.

You should have installed at least a modern evergreen web browser, such as Internet Explorer 10 or above (http://support.microsoft.com/product/internet-explorer/internet-explorer-10/), Google Chrome (http://www.google.com/chrome/), or Firefox (https://www.mozilla.org/en-US/firefox/new/).

JavaScript is an interpreted language, so you do not need any special IDE or editor; any editor that supports simple plain text will do. Having said that, I recommend using an advanced editor such as vi, vim, TextMate (http://macromates.com/), Sublime (http://www.sublimetext.com/), or Atom (https://atom.io/). If you prefer an IDE, you can try WebStorm (https://www.jetbrains.com/webstorm/download/), although a full-fledged IDE is not needed.

During the book, especially in Chapter 2, Automating Tests with Mocha, Chai, and Sinon, detailed explanations are given about how to install and configure the necessary software and tools. This includes Node.js, WebDriver, and all the libraries we are going to use. All of them are open source and free-of-charge.

Who this book is for

This book is for any JavaScript developer who is interested in producing well-tested code. If you have no prior experience with testing Node.js, or any other tool, do not worry as they will be explained from scratch. Even if you have already used some of the tools explored in the book it can still help you to learn additional testing techniques and best practices.

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: "The headers property of the replay object is an array of regular expressions."

A block of code is set as follows:

var result = b.operation(1, 2);

expect(result).to.be.deep.equal({
  args: [1, 2],
  result: 3
});

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

module.exports = function (findConfiguration) {
  return function () {
    findConfiguration('default');
    return validatorWith([
      nonPositiveValidationRule,
      nonDivisibleValidationRule(3, 'error.three'),
      nonDivisibleValidationRule(5, 'error.five')
    ]);
  };
};

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

$ me@~> mkdir validator
$ me@~> cd validator
$ me@~/validator> npm init

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: "You can download and execute a nice installer by going to Node.js website and clicking on Install."

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.