Introduction |
|
xxi | |
Part I: Laying A Solid Foundation |
|
|
Chapter 1 Practicing Skillful Software |
|
|
3 | (26) |
|
Writing Code That Starts Correct |
|
|
4 | (18) |
|
Mastering the Features of JavaScript |
|
|
4 | (12) |
|
|
5 | (10) |
|
JavaScript Is Single-Threaded |
|
|
15 | (1) |
|
Avoiding JavaScript's Pitfalls in Larger Systems |
|
|
16 | (2) |
|
|
16 | (1) |
|
Nested Functions Control Scope |
|
|
16 | (1) |
|
|
17 | (1) |
|
Applying the Principles of Software Engineering |
|
|
18 | (4) |
|
|
18 | (3) |
|
|
21 | (1) |
|
Writing Code That Stays Correct |
|
|
22 | (5) |
|
Investing for the Future with Unit Tests |
|
|
22 | (1) |
|
Practicing Test-Driven Development |
|
|
23 | (1) |
|
Engineering Your Code to Be Easy to Test |
|
|
24 | (3) |
|
|
27 | (2) |
|
|
29 | (52) |
|
Using a Testing Framework |
|
|
29 | (16) |
|
Identifying Incorrect Code |
|
|
33 | (2) |
|
Designing for Testability |
|
|
35 | (1) |
|
Writing the Minimum Required Code |
|
|
36 | (1) |
|
Safe Maintenance and Refactoring |
|
|
36 | (1) |
|
|
37 | (1) |
|
Current Open-Source and Commercial Frameworks |
|
|
37 | (2) |
|
|
37 | (1) |
|
|
38 | (1) |
|
|
39 | (6) |
|
|
39 | (2) |
|
Expectations and Matchers |
|
|
41 | (1) |
|
|
41 | (4) |
|
Using a Dependency-Injection Framework |
|
|
45 | (13) |
|
What Is Dependency Injection? |
|
|
45 | (1) |
|
Making Your Code More Reliable with Dependency Injection |
|
|
46 | (1) |
|
Mastering Dependency Injection |
|
|
47 | (1) |
|
Case Study: Writing a Lightweight Dependency-Injection Framework |
|
|
47 | (8) |
|
Using a Dependency-Injection Framework |
|
|
55 | (1) |
|
Current Dependency-Injection Frameworks |
|
|
56 | (2) |
|
|
56 | (1) |
|
|
57 | (1) |
|
|
58 | (15) |
|
Case Study: Caching with and without AOP |
|
|
58 | (2) |
|
Implementing Caching without AOP |
|
|
58 | (2) |
|
Making Your Code More Reliable with AOP |
|
|
60 | (1) |
|
Case Study: Building the Aop.js Module |
|
|
60 | (12) |
|
|
72 | (1) |
|
|
72 | (1) |
|
|
73 | (1) |
|
|
73 | (1) |
|
|
73 | (1) |
|
Using a Code-Checking Tool |
|
|
73 | (7) |
|
Making Your Code More Reliable with Linting Tools |
|
|
74 | (2) |
|
|
76 | (3) |
|
|
76 | (3) |
|
If You Don't Run It, Bugs Will Come |
|
|
79 | (1) |
|
|
79 | (1) |
|
|
79 | (1) |
|
|
79 | (1) |
|
|
80 | (1) |
|
|
80 | (1) |
|
Chapter 3 Constructing Reliable Objects |
|
|
81 | (26) |
|
|
81 | (2) |
|
|
83 | (1) |
|
|
84 | (3) |
|
|
84 | (1) |
|
Creating Immediate-Execution Modules |
|
|
85 | (1) |
|
Creating Reliable Modules |
|
|
86 | (1) |
|
Using Object Prototypes and Prototypal Inheritance |
|
|
87 | (2) |
|
The Default Object Prototype |
|
|
87 | (1) |
|
|
88 | (1) |
|
|
88 | (1) |
|
Creating Objects with New |
|
|
89 | (6) |
|
The new Object Creation Pattern |
|
|
89 | (6) |
|
Potential for Bad Things to Happen |
|
|
90 | (1) |
|
|
90 | (5) |
|
Using Classical Inheritance |
|
|
95 | (3) |
|
Emulating Classical Inheritance |
|
|
95 | (1) |
|
Repetition Killed the Kangaroo |
|
|
96 | (2) |
|
Using Functional Inheritance |
|
|
98 | (2) |
|
|
100 | (2) |
|
|
102 | (5) |
Part II: Testing Pattern-Based Code |
|
|
Chapter 4 Reviewing The Benefits Of Patterns |
|
|
107 | (4) |
|
|
107 | (1) |
|
Producing More Elegant Code by Using a Broader Vocabulary |
|
|
108 | (1) |
|
Producing Reliable Code with Well-Engineered, Well-Tested Building Blocks |
|
|
109 | (1) |
|
|
110 | (1) |
|
Chapter 5 Ensuring Correct Use Of The Callback Pattern |
|
|
111 | (18) |
|
Understanding the Pattern Through Unit Tests |
|
|
112 | (9) |
|
Writing and Testing Code That Uses Callback Functions |
|
|
112 | (5) |
|
Writing and Testing Callback Functions |
|
|
117 | (4) |
|
|
121 | (7) |
|
Flattening the Callback Arrow |
|
|
121 | (2) |
|
|
123 | (5) |
|
|
128 | (1) |
|
Chapter 6 Ensuring Correct Use Of The Promise Pattern |
|
|
129 | (16) |
|
Understanding Promises Through Unit Tests |
|
|
130 | (11) |
|
|
130 | (5) |
|
Constructing and Returning a Promise |
|
|
135 | (3) |
|
Testing an XMLHttpRequest |
|
|
138 | (3) |
|
|
141 | (1) |
|
|
142 | (1) |
|
Understanding States and Fates |
|
|
143 | (1) |
|
Distinguishing Standard Promises from jQuery Promises |
|
|
143 | (1) |
|
|
143 | (2) |
|
Chapter 7 Ensuring Correct Use Of Partial Function Application |
|
|
145 | (6) |
|
Unit-Testing a Partial Function Application |
|
|
145 | (2) |
|
Creating an Aspect for Partial Function Application |
|
|
147 | (2) |
|
Distinguishing Between Partial Function Application and Currying |
|
|
149 | (1) |
|
|
149 | (1) |
|
Partial Function Application |
|
|
149 | (1) |
|
|
150 | (1) |
|
Chapter 8 Ensuring Correct Use Of The Memoization Pattern |
|
|
151 | (10) |
|
Understanding the Pattern Through Unit Tests |
|
|
152 | (3) |
|
Adding Memoization with AOP |
|
|
155 | (5) |
|
Creating the Memoization Aspect |
|
|
155 | (4) |
|
Applying the returnValueCache Aspect to restaurantApi |
|
|
159 | (1) |
|
|
160 | (1) |
|
Chapter 9 Ensuring Correct Implementation Of The Singleton Pattern |
|
|
161 | (12) |
|
Understanding the Pattern Through Unit Tests |
|
|
162 | (8) |
|
Implementing a Singleton Shared Cache with an Object Literal |
|
|
162 | (4) |
|
Implementing a Singleton Shared Cache with a Module |
|
|
166 | (4) |
|
|
170 | (3) |
|
Chapter 10 Ensuring Correct Implementation Of The Factory Pattern |
|
|
173 | (10) |
|
Writing Unit Tests for a Factory |
|
|
173 | (6) |
|
Implementing the Factory Pattern |
|
|
179 | (2) |
|
Considering Other Factory Types |
|
|
181 | (1) |
|
|
181 | (2) |
|
Chapter 11 Ensuring Correct Implementation And Use Of The Sandbox Pattern |
|
|
183 | (22) |
|
Understanding the Pattern Through Unit Tests |
|
|
184 | (19) |
|
Creating a Widget Sandbox |
|
|
185 | (12) |
|
Instantiating a Widget Sandbox |
|
|
185 | (2) |
|
Providing Tools to the Widget via the Sandbox |
|
|
187 | (10) |
|
Creating and Testing Sandbox Tools |
|
|
197 | (4) |
|
Creating Functions for Use with a Sandbox |
|
|
201 | (2) |
|
|
203 | (2) |
|
Chapter 12 Ensuring Correct Implementation Of The Decorator Pattern |
|
|
205 | (18) |
|
Developing a Decorator the Test-Driven Way |
|
|
207 | (15) |
|
Writing a Fake for the Decorated Object |
|
|
207 | (1) |
|
Writing Tests for Pass-Through of Errors |
|
|
208 | (1) |
|
Writing a Do-Nothing Decorator |
|
|
209 | (1) |
|
Adding Pass-Through Functionality to the Decorator |
|
|
210 | (3) |
|
Verifying Pass-Through of Successes |
|
|
213 | (2) |
|
Adding the Decorator's Features |
|
|
215 | (7) |
|
Generalizing the Decorator |
|
|
222 | (1) |
|
|
222 | (1) |
|
Chapter 13 Ensuring Correct Implementation Of The Strategy Pattern |
|
|
223 | (16) |
|
Understanding the Pattern Through Unit Tests |
|
|
223 | (14) |
|
Implementing the transportScheduler Without the Strategy Pattern |
|
|
224 | (2) |
|
Implementing the transportScheduler Using the Strategy Pattern |
|
|
226 | (49) |
|
Creating transportScheduler Using Test-Driven Development |
|
|
227 | (8) |
|
Creating a Strategy for Use with transportScheduler |
|
|
235 | (2) |
|
|
237 | (2) |
|
Chapter 14 Ensuring Correct Implementation Of The Proxy Pattern |
|
|
239 | (18) |
|
Developing a Proxy the Test-Driven Way |
|
|
240 | (16) |
|
|
256 | (1) |
|
Chapter 15 Ensuring Correct Implementation Of Chainable Methods |
|
|
257 | (14) |
|
Understanding the Pattern Through Unit Tests |
|
|
259 | (7) |
|
|
266 | (1) |
|
|
267 | (4) |
Part III: Testing And Writing With Advanced Javascript Features |
|
|
Chapter 16 Conforming To Interfaces In An Interface-Free Language |
|
|
271 | (18) |
|
Understanding the Benefits of Interfaces |
|
|
272 | (1) |
|
Understanding the Interface Segregation Principle |
|
|
273 | (2) |
|
Using Test-Driven Development to Create a Contract Registry |
|
|
275 | (13) |
|
|
275 | (3) |
|
Determining Whether a Contract Is Fulfilled |
|
|
278 | (4) |
|
Asserting That a Contract Is Fulfilled |
|
|
282 | (1) |
|
Bypassing Contract Enforcement |
|
|
283 | (1) |
|
Creating an Aspect to Enforce a Contract on a Returned (Created) Object |
|
|
283 | (5) |
|
|
288 | (1) |
|
Chapter 17 Ensuring Correct Argument Types |
|
|
289 | (22) |
|
|
289 | (1) |
|
Understanding the Opportunities and Risks Posed by JavaScript's Type-Free Parameters |
|
|
290 | (1) |
|
Extending the ContractRegistry to Check Arguments |
|
|
290 | (13) |
|
|
291 | (1) |
|
Determining Whether Every Variable in a Set Fulfills Its Contract |
|
|
291 | (9) |
|
Asserting That Every Variable in a Set Fulfills Its Contract |
|
|
300 | (1) |
|
Packaging Argument-Checking in an Aspect |
|
|
301 | (2) |
|
Supporting Contract Libraries |
|
|
303 | (1) |
|
|
303 | (4) |
|
Creating the Contracts Modules |
|
|
304 | (3) |
|
Creating the Application's ContractRegistry |
|
|
307 | (1) |
|
Bypassing Contracts for Production |
|
|
307 | (1) |
|
Comparing the Aspect-Oriented Solution to a Static Solution |
|
|
307 | (1) |
|
Considering the Advantages of TypeScript |
|
|
308 | (1) |
|
Considering the Advantages of Aspects |
|
|
308 | (1) |
|
|
308 | (3) |
|
Chapter 18 Ensuring Correct Use Of Call, Apply, And Bind |
|
|
311 | (24) |
|
Exploring How this Is Bound |
|
|
312 | (5) |
|
|
312 | (2) |
|
Default Binding and strict Mode |
|
|
313 | (1) |
|
|
314 | (2) |
|
|
316 | (1) |
|
|
317 | (1) |
|
Creating and Testing Code That Uses call, apply, and bind |
|
|
317 | (17) |
|
|
318 | (2) |
|
Creating an Array.prototype.forEach Polyfill Using Test-Driven Development |
|
|
320 | (9) |
|
|
329 | (5) |
|
|
334 | (1) |
|
Chapter 19 Ensuring The Correct Use Of Method-Borrowing |
|
|
335 | (18) |
|
Ensuring the Borrowing Object Is Suitable |
|
|
336 | (7) |
|
Making the Borrowed Function Qualify the Borrower |
|
|
336 | (2) |
|
Attaching an Aspect to the Borrowed Object |
|
|
338 | (4) |
|
|
342 | (1) |
|
Adding an Object-Validator to the ContractRegistry |
|
|
342 | (1) |
|
Anticipating Side Effects on the Borrower |
|
|
343 | (8) |
|
Considering Side Effects from an Isolated Function |
|
|
343 | (2) |
|
Considering Side Effects from a Function That Calls Other Functions |
|
|
345 | (6) |
|
Anticipating Side Effects on the Donor Object |
|
|
351 | (1) |
|
|
351 | (2) |
|
Chapter 20 Ensuring Correct Use Of Mixins |
|
|
353 | (30) |
|
Creating and Using Mixins |
|
|
355 | (25) |
|
Creating and Using a Traditional Mixin |
|
|
356 | (17) |
|
Creating the extend Function Using Test-driven Development |
|
|
356 | (11) |
|
Creating a Traditional Mixin Using Test-driven Development |
|
|
367 | (6) |
|
Creating and Using a Functional Mixin |
|
|
373 | (7) |
|
|
380 | (3) |
|
Chapter 21 Testing Advanced Program Architectures |
|
|
383 | (30) |
|
Ensuring Reliable Use of the Observer Pattern |
|
|
384 | (11) |
|
Examining the Observer Pattern |
|
|
384 | (7) |
|
Enhancing the Reliability of the Observer Pattern |
|
|
391 | (4) |
|
Ensuring Reliable Use of the Mediator Pattern |
|
|
395 | (15) |
|
Examining the Mediator Pattern |
|
|
396 | (1) |
|
Enhancing the Reliability of Mediator-Based Code |
|
|
397 | (17) |
|
|
398 | (1) |
|
|
399 | (3) |
|
Segregating the Mediator's Interfaces |
|
|
402 | (1) |
|
Deciding Where to Put the Contracts |
|
|
403 | (1) |
|
Ensuring the Colleague Gets a Mediator with the Expected Interface |
|
|
404 | (2) |
|
|
406 | (2) |
|
|
408 | (2) |
|
|
410 | (3) |
Part IV: Special Subjects In Testing |
|
|
Chapter 22 Testing DOM Access |
|
|
413 | (22) |
|
|
414 | (11) |
|
Examining Difficult-to-Test UI Code |
|
|
414 | (2) |
|
Creating a UI Component Using TDD |
|
|
416 | (9) |
|
Testing Code That Changes the DOM |
|
|
418 | (4) |
|
Testing to Ensure Event Handlers Are Executed |
|
|
422 | (3) |
|
Keeping UI Tests from Being Brittle |
|
|
425 | (1) |
|
Optimizing Your Code with a Profiler |
|
|
425 | (8) |
|
|
426 | (7) |
|
Avoiding Premature Optimization |
|
|
433 | (1) |
|
|
433 | (2) |
|
Chapter 23 Ensuring Conformance To Standards |
|
|
435 | (30) |
|
|
436 | (13) |
|
|
436 | (3) |
|
|
436 | (3) |
|
Installing ESLint Using npm |
|
|
439 | (1) |
|
|
439 | (5) |
|
Executing ESLint on a Single File |
|
|
442 | (1) |
|
Executing ESLint on All the JavaScript Files in a Directory |
|
|
443 | (1) |
|
Enforcing Coding Standards with ESLint |
|
|
444 | (5) |
|
Creating a Custom ESLint Rule |
|
|
445 | (3) |
|
Running ESLint with Custom Rules |
|
|
448 | (1) |
|
Enforcing Architectural Divisions |
|
|
449 | (11) |
|
The Family-Secret Technique |
|
|
450 | (2) |
|
|
452 | (2) |
|
The Mission Impossible Technique |
|
|
454 | (5) |
|
|
459 | (1) |
|
Do Not Use the Call Stack Technique |
|
|
460 | (1) |
|
|
460 | (1) |
|
|
460 | (1) |
|
|
460 | (5) |
Part V: Summary |
|
|
Chapter 24 Summary Of The Principles Of Test-Driven Development |
|
|
465 | (10) |
|
Recalling Why Test-Driven Development Is Worthwhile |
|
|
465 | (1) |
|
Practicing Test-Driven Development |
|
|
466 | (1) |
|
Writing Unit-Testable Code |
|
|
466 | (1) |
|
Mastering the Mechanics of Test-Driven Development |
|
|
466 | (2) |
|
Writing the Test Before the Code |
|
|
467 | (1) |
|
|
467 | (1) |
|
Testing Error Conditions First |
|
|
467 | (1) |
|
Testing the Simple before the Complex |
|
|
467 | (1) |
|
|
467 | (1) |
|
|
468 | (1) |
|
Your Test Data Are Just As Important As the Test |
|
|
468 | (1) |
|
Using Jasmine Effectively |
|
|
468 | (1) |
|
Testing the Patterns in This Book |
|
|
468 | (5) |
|
Testing Aspect-Oriented Programming |
|
|
468 | (1) |
|
Testing Object Construction |
|
|
469 | (1) |
|
|
469 | (1) |
|
Testing Promise-Based Code |
|
|
469 | (1) |
|
Testing a Partial Function Application |
|
|
470 | (1) |
|
|
470 | (1) |
|
|
470 | (1) |
|
|
470 | (1) |
|
|
470 | (1) |
|
Testing the Decorator Pattern |
|
|
471 | (1) |
|
Testing the Strategy Pattern |
|
|
471 | (1) |
|
Testing the Proxy Pattern |
|
|
471 | (1) |
|
Testing Chainable Methods |
|
|
471 | (1) |
|
Testing Conformance to an Interface |
|
|
472 | (1) |
|
Testing the Use of call and apply |
|
|
472 | (1) |
|
Testing the Method-Borrowing Pattern |
|
|
472 | (1) |
|
|
472 | (1) |
|
Testing Mediators and Observers |
|
|
473 | (1) |
|
|
473 | (1) |
|
Tests to Enforce Architectural Divisions |
|
|
473 | (1) |
|
|
473 | (2) |
|
Chapter 25 Summary Of Javascript Idioms In This Book |
|
|
475 | (14) |
|
|
475 | (2) |
|
Object Properties May Be Added and Removed |
|
|
476 | (1) |
|
Objects May Be Used as a Dictionary |
|
|
476 | (1) |
|
|
477 | (4) |
|
Variable Declarations Are Hoisted |
|
|
477 | (1) |
|
Variables Have Function Scope |
|
|
478 | (3) |
|
|
481 | (5) |
|
|
481 | (1) |
|
Functions Declarations Are Hoisted |
|
|
481 | (1) |
|
Functions Don't Have Return Types |
|
|
482 | (1) |
|
Functions May Be Anonymous |
|
|
482 | (1) |
|
|
483 | (1) |
|
Functions May Be Invoked with Any Number of Arguments |
|
|
484 | (1) |
|
Functions May Be Invoked Immediately |
|
|
485 | (1) |
|
Reviewing Boolean Operations |
|
|
486 | (1) |
|
Types May Be Coerced When Testing Equality |
|
|
486 | (1) |
|
Values May Be Truthy or Falsy |
|
|
487 | (1) |
|
|
487 | (2) |
Index |
|
489 | |