Early praise for Test-Driving JavaScript Applications Not only does this book provide step-by-step instructions for how to test JavaScript, Venkat also presents a simplified, well-rounded approach to driving out good de- sign. Venkat writes with a compelling and humorous voice that makes the content educational and entertaining. ➤ Kimberly D. Barnes Senior software engineer, GoSpotCheck Venkat has, in his usual inimitable style, taken the swamp of JavaScript testing and drained it into a clear pool. He has taken a welcome pragmatic approach to the subject of testing this sometimes tricky platform, providing frameworks, tools, and valuable tips and insights along the way. Highly recommended. ➤ Neal Ford Director/software architect/meme wrangler, Thoughtworks, Inc. If you are a seasoned TDD practitioner in another language, this book answers all the questions to test-drive JavaScript applications. If you are a JavaScript programmer who hasn’t yet embraced TDD, Venkat takes you through several real-world examples to get you started with confidence. ➤ Naresha K. Chief technologist, Channel Bridge Software Labs Test-Driving JavaScript Applications has shown me how to use TDD for the front end. This book was exactly what I was looking for. It has practical examples and lots of useful information. I’ve learned good practices, and I’ve stopped writing legacy code. ➤ Astefanoaie Nicolae Stelian Senior programmer, PRISA Test-Driving JavaScript Applications Rapid, Confident, Maintainable Code Venkat Subramaniam The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Jacquelyn Carter (editor) Potomac Indexing, LLC (index) Liz Welch (copyedit) Gilson Graphics (layout) Janet Furlow (producer) For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2016 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-68050-174-2 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—October 2016 In loving memory of professor and uncle PSK, for lighting my passion for programming. Contents Acknowledgments . . . . . . . . . . . xi Introduction . . . . . . . . . . . . xiii 1. Automation Shall Set You Free . . . . . . . . 1 The Challenges of Change 1 Testing vs. Verification 2 Adopting Automated Verification 3 Why Is It Hard to Verify? 6 How to Approach Automated Testing 7 Wrapping Up 8 Part I — Creating Automated Tests 2. Test-Drive Your Design . . . . . . . . . . 11 Let’s Get Started 12 Design with Positive, Negative, and Exception Tests 21 Design Server-Side Code 23 Measure Server-Side Code Coverage 33 Prepare for Client-Side Testing 36 Design Client-Side Code 40 Measure Client-Side Code Coverage 43 Wrapping Up 44 3. Test Asynchrony . . . . . . . . . . . 45 Server-Side Callbacks 46 Client-Side Callbacks 51 Test Your Promises 54 Wrapping Up 60 Contents • viii 4. Tactfully Tackle Dependencies . . . . . . . . 61 A Problem and Spiking to Learn 61 Visualize a Modular Design 64 Separate Dependencies Where Possible 65 Employ Test Doubles 68 Inject the Dependencies 71 Test Interactions 72 Use Sinon to Reduce Noise 75 Review and Run 84 Wrapping Up 88 Part II — Real-World Automated Testing 5. Test-Drive Node.js Apps . . . . . . . . . . 91 Start with a Strategic—Just Enough—Design 91 Leap Into Tactical—Test First—Design 93 Continue the Design 99 Create a Spike to Gain Insight 102 Modularize to Facilitate Testing 108 Separate Concerns 112 Integrate and Run 114 Review the Coverage and Design 117 Providing HTTP Access 119 Wrapping Up 120 6. Test-Drive Express Apps . . . . . . . . . 121 Design for Testability 121 Set Up the Express App and Run a Canary Test 124 Design the Database Connection 125 Design the Model 129 Design the Routes 144 Measure Code Coverage 154 Take It for a Drive 156 Wrapping Up 160 7. Working with the DOM and jQuery . . . . . . . 161 Create a Strategic Design 161 Create the Tactical Design with Tests 162 Evolve Code in Small Steps 164 Take the UI for a Short Drive 175
Description: