Book Image

Test-Driven JavaScript Development

By : Ravi Kumar Gupta
Book Image

Test-Driven JavaScript Development

By: Ravi Kumar Gupta

Overview of this book

Initially, all processing used to happen on the server-side and simple output was the response to web browsers. Nowadays, there are so many JavaScript frameworks and libraries created that help readers to create charts, animations, simulations, and so on. By the time a project finishes or reaches a stable state, so much JavaScript code has already been written that changing and maintaining it further is tedious. Here comes the importance of automated testing and more specifically, developing all that code in a test-driven environment. Test-driven development is a methodology that makes testing the central part of the design process – before writing code developers decide upon the conditions that code must meet to pass a test. The end goal is to help the readers understand the importance and process of using TDD as a part of development. This book starts with the details about test-driven development, its importance, need, and benefits. Later the book introduces popular tools and frameworks like YUI, Karma, QUnit, DalekJS, JsUnit and goes on to utilize Jasmine, Mocha, Karma for advanced concepts like feature detection, server-side testing, and patterns. We are going to understand, write, and run tests, and further debug our programs. The book concludes with best practices in JavaScript testing. By the end of the book, the readers will know why they should test, how to do it most efficiently, and will have a number of versatile tests (and methods for devising new tests) to get to work immediately.
Table of Contents (16 chapters)
Test-Driven JavaScript Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Initially, all processing used to happen on the server's side and simple output was the response to web browsers. Nowadays, there are so many JavaScript frameworks and libraries that help readers to create charts, animations, simulations, and so on. By the time a project finishes or reaches a stable state, so much JavaScript code has already been written that changing and maintaining it further is tedious. At this point comes the importance of automated testing, and more specifically, developing all that code in a test-driven environment. Test-driven development is a methodology that makes testing the central part of the design process—before writing code, developers decide upon the conditions that code must meet to pass a test. The end goal is to help the readers understand the importance and process of using TDD as a part of the development.

This book starts with the details of test-driven development, its importance, need, and benefits. Later, the book introduces popular tools and frameworks, such as YUI, Karma, QUnit, DalekJS, JsUnit, and so on, to utilize Jasmine, Mocha, and Karma for advanced concepts such as feature detection, server-side testing, and patterns. We are going to understand, write, run tests, and further debug our programs. The book concludes with best practices in JavaScript testing. By the end of the book, the readers will know why they should test, how to do it most efficiently, and will have a number of versatile tests (and methods to devise new tests) so they can get to work immediately.

What this book covers

Chapter 1, Overview of TDD, introduces the test-driven development, its life cycle, benefits, and myths.

Chapter 2, Testing Concepts, brings the TDD life cycle into action using Yahoo User Interface (YUI) Tests, and explains how unit testing can be done for JavaScript.

Chapter 3, Testing Tools, introduces JsUnit, QUnit, Karma, and DalekJS, which are some of the popular unit testing frameworks for JavaScript.

Chapter 4, Jasmine, introduces behavior-driven development and the Jasmine framework, its setup, usage, and customization, along with several features that a good unit testing framework should cover.

Chapter 5, JsTestDriver, showcases the JsTestDriver unit testing tool and its integration with IDE.

Chapter 6, Feature Detection, explores has.js and Modernizr JavaScript libraries for feature detection and explains why feature detection should have preference over browser detection.

Chapter 7, Observer Design Pattern, explains the observer pattern for JavaScript and its role in the test-driven development.

Chapter 8, Testing with Server-Side JS, covers server-side JavaScript unit testing using Node.js, Mocha, and Chai while using MongoDB as a database.

Chapter 9, Best Practices, lists best practices used to unit test JavaScript and also helps to make a good choice among popular unit testing frameworks and tools by explaining the features.

Who this book is for

If you have an intermediate knowledge of HTML, CSS, and JavaScript and want to learn how and why the test-driven development approach is better for your assignments, then this book is for you.

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: "This will start a server on port 9880 keeping runner mode QUIET as you can see in the command prompt after running the command."

A block of code is set as follows:

<script>
YUI().use('test-console','test', function (Y) {
      var testCase = new Y.Test.Case({
        /*
        * Sets up data needed by each test.

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

toCurrencyAmount = rateOfConversion* amount;

// rounding off
toCurrencyAmount = Number.parseFloat(toCurrencyAmount).toFixed(2);

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

npm install karma –save-dev

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: "Click on the first link Capture This Browser, which will allow JsTestDriver to capture this browser."

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.