文件名称:Test-Driven JavaScript Development
文件大小:2.92MB
文件格式:PDF
更新时间:2021-02-11 08:30:23
Test-Driven JavaScript
What This Book is About This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage, and gaining the ability to fearlessly refactor and organically evolve your code base. It is about writing modular and testable code. It is about writing JavaScript that works in a wide variety of environments and that doesn’t get in your user’s way. How This Book is Organized This book has four parts. They may be read in any order you’re comfortable with. Part II introduces a few utilities that are used throughout the book, but their usage should be clear enough, allowing you to skip that part if you already have a solid understanding of programming JavaScript, including topics such as unobtrusive JavaScript and feature detection. Part I: Test-Driven Development In the first part I’ll introduce you to the concept of automated tests and test-driven development. We’ll start by looking at what a unit test is, what it does, and what it’s good for. Then we’ll build our workflow around them as I introduce the test- driven development process. To round the topic off I’ll show you a few available unit testing frameworks for JavaScript, discuss their pros and cons, and take a closer look at the one we’ll be using the most throughout the book. Part II: JavaScript for Programmers In Part II we’re going to get a deeper look at programming in JavaScript. This part is by no means a complete introduction to the JavaScript language. You should already either have some experience with JavaScript—perhaps by working with libraries like jQuery, Prototype, or the like—or experience from other programming languages. If you’re an experienced programmer with no prior experience with JavaScript, this part should help you understand where JavaScript differs from other languages, especially less dynamic ones, and give you the foundation you’ll need for the real- world scenarios in Part III. If you’re already well-versed in advanced JavaScript concepts such as closures, prototypal inheritance, the dynamic nature of this, and feature detection, you may want to skim this part for a reminder, or you may want to skip directly to Part III. While working through some of JavaScript’s finer points, I’ll use unit tests to show you how the language behaves, and we’ll take the opportunity to let tests drive us through the implementation of some helper utilities, which we’ll use throughout Part III. Part III: Real-World Test-Driven Development in JavaScript In this part we’ll tackle a series of small projects in varying environments. We’ll see how to develop a small general purpose JavaScript API, develop a DOM dependent widget, abstract browser differences, implement a server-side JavaScript application, and more—all using test-driven development. This part focuses on how test-driven development can help in building cleaner API’s, better modularized code and more robust software. Each project introduces new test-related concepts, and shows them in practice by implementing a fully functional, yet limited piece of code. Throughout this part we will, among other things, learn how to test code that depends on browser API’s, timers, event handlers, DOM manipulation, and asynchronous server requests (i.e., “Ajax”). We will also get to practice techniques such as stubbing, refactoring, and using design patterns to solve problems in elegant ways. Throughout each chapter in this part, ideas on how to extend the functionality developed are offered, giving you the ability to practice by improving the code on your own. Extended solutions are available from the book’s website.1 I’ve taken great care throughout these projects to produce runnable code that actually does things. The end result of the five chapters in Part III is a fully func- tional instant messaging chat client and server, written exclusively using test-driven development, in nothing but JavaScript. Part IV: Testing Patterns The final part of the book reviews some of the techniques used throughout Part III from a wider angle. Test doubles, such as mocks and stubs, are investigated in closer detail along with different forms of test verification. Finally, we review some guidelines to help you write good unit tests.