For JavaScript developers working on increasingly large and complex projects, effective automated testing is crucial to success. Test-Driven JavaScript Development is a complete, best-practice guide to agile JavaScript testing and quality assurance with the test-driven development (TDD) methodology. Leading agile JavaScript developer Christian Johansen covers all aspects of applying state-of-the-art automated testing in JavaScript environments, walking readers through the entire development lifecycle, from project launch to application deployment, and beyond. Using real-life examples driven by unit tests, Johansen shows how to use TDD to gain greater confidence in your code base, so you can fearlessly refactor and build more robust, maintainable, and reliable JavaScript code at lower cost. Throughout, he addresses crucial issues ranging from code design to performance optimization, offering realistic solutions for developers, QA specialists, and testers. Coverage includes• Understanding automated testing and TDD • Building effective automated testing workflows• Testing code for both browsers and servers (using Node.js)• Using TDD to build cleaner APIs, better modularized code, and more robust software• Writing testable code• Using test stubs and mocks to test units in isolation• Continuously improving code through refactoring• Walking through the construction and automated testing of fully functional softwareThe accompanying Web site, tddjs.com, contains all of the book’s code listings and additional resources.
Author(s): Christian Johansen
Edition: 1
Publisher: Addison-Wesley Professional
Year: 2010
Language: English
Pages: 525
Contents......Page 7
Preface......Page 19
Acknowledgments......Page 25
About the Author......Page 27
Part I: Test-Driven Development......Page 29
1. Automated Testing......Page 31
1.1 The Unit Test......Page 32
1.2 Assertions......Page 37
1.3 Test Functions, Cases and Suites......Page 39
1.4 Integration Tests......Page 42
1.5 Benefits of Unit Tests......Page 44
1.7 Summary......Page 46
2.1 Goal and Purpose of Test-Driven Development......Page 49
2.2 The Process......Page 51
2.3 Facilitating Test-Driven Development......Page 57
2.4 Benefits of Test-Driven Development......Page 58
2.5 Summary......Page 59
3.1 xUnit Test Frameworks......Page 61
3.2 In-Browser Test Frameworks......Page 65
3.3 Headless Testing Frameworks......Page 69
3.4 One Test Runner to Rule Them All......Page 70
3.5 Summary......Page 80
4.1 Exploring JavaScript with Unit Tests......Page 83
4.2 Performance Tests......Page 88
4.3 Summary......Page 97
Part II: JavaScript for Programmers......Page 99
5.1 Defining Functions......Page 101
5.2 Calling Functions......Page 105
5.3 Scope and Execution Context......Page 108
5.4 The this Keyword......Page 115
5.5 Summary......Page 119
6.1 Binding Functions......Page 121
6.2 Immediately Called Anonymous Functions......Page 129
6.3 Stateful Functions......Page 135
6.4 Memoization......Page 140
6.5 Summary......Page 143
7.1 Objects and Properties......Page 145
7.2 Creating Objects with Constructors......Page 158
7.3 Pseudo-classical Inheritance......Page 164
7.4 Encapsulation and Information Hiding......Page 173
7.5 Object Composition and Mixins......Page 178
7.6 Summary......Page 186
8.1 The Close Future of JavaScript......Page 187
8.2 Updates to the Object Model......Page 189
8.3 Strict Mode......Page 199
8.4 Various Additions and Improvements......Page 202
8.5 Summary......Page 204
9.1 The Goal of Unobtrusive JavaScript......Page 205
9.2 The Rules of Unobtrusive JavaScript......Page 206
9.3 Do Not Make Assumptions......Page 211
9.4 When Do the Rules Apply?......Page 212
9.5 Unobtrusive Tabbed Panel Example......Page 213
9.6 Summary......Page 224
10. Feature Detection......Page 225
10.1 Browser Sniffing......Page 226
10.2 Using Object Detection for Good......Page 228
10.3 Feature Testing DOM Events......Page 235
10.4 Feature Testing CSS Properties......Page 236
10.5 Cross-Browser Event Handlers......Page 238
10.6 Using Feature Detection......Page 241
10.7 Summary......Page 242
Part III: Real-World Test-Driven Development in JavaScript......Page 245
11. The Observer Pattern......Page 247
11.1 The Observer in JavaScript......Page 248
11.2 Adding Observers......Page 250
11.3 Checking for Observers......Page 254
11.4 Notifying Observers......Page 258
11.5 Error Handling......Page 260
11.6 Observing Arbitrary Objects......Page 263
11.7 Observing Arbitrary Events......Page 269
11.8 Summary......Page 274
12.1 Test Driving a Request API......Page 275
12.2 Implementing the Request Interface......Page 277
12.3 Creating an XMLHttpRequest Object......Page 278
12.4 Making Get Requests......Page 283
12.5 Using the Ajax API......Page 297
12.6 Making POST Requests......Page 305
12.7 Reviewing the Request API......Page 316
12.8 Summary......Page 320
13. Streaming Data with Ajax and Comet......Page 321
13.1 Polling for Data......Page 322
13.2 Comet......Page 342
13.3 Long Polling XMLHttpRequest......Page 343
13.4 The Comet Client......Page 349
13.5 Summary......Page 367
14.1 The Node.js Runtime......Page 369
14.2 The Controller......Page 373
14.3 Domain Model and Storage......Page 386
14.4 Promises......Page 395
14.5 Event Emitters......Page 400
14.6 Returning to the Controller......Page 406
14.7 Summary......Page 415
15.1 Planning the Client......Page 417
15.2 The User Form......Page 420
15.3 Using the Client with the Node.js Backend......Page 436
15.4 The Message List......Page 439
15.5 The Message Form......Page 450
15.6 The Final Chat Client......Page 457
15.7 Summary......Page 462
PartIV: Testing Patterns......Page 465
16.1 An Overview of Test Doubles......Page 467
16.2 Test Verification......Page 469
16.3 Stubs......Page 471
16.4 Test Spies......Page 473
16.5 Using a Stub Library......Page 475
16.6 Mocks......Page 481
16.7 Mocks or Stubs?......Page 485
16.8 Summary......Page 486
17. Writing Good Unit Tests......Page 489
17.1 Improving Readability......Page 490
17.2 Tests as Behavior Specification......Page 496
17.3 Fighting Bugs in Tests......Page 501
17.4 Summary......Page 503
Bibliography......Page 505
A......Page 507
C......Page 509
D......Page 511
E......Page 512
G......Page 513
I......Page 514
L......Page 515
N......Page 516
O......Page 517
P......Page 518
S......Page 520
T......Page 522
U......Page 523
V......Page 524
Z......Page 525