TAILIEUCHUNG - Test Driven JavaScript Development- P25

Test Driven JavaScript Development- P25: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. | Fighting Bugs in Tests 473 Fighting Bugs in Tests Developers who are unfamiliar with unit testing often ask how do you test your tests The answer of course is that we don t. That does not imply that we do not take measures to reduce defects in tests. The most important way to reduce the chance of bugs in tests is to never implement logic in tests. A unit test should be a simple sequence of assignments and function calls followed by one or more assertions. Apart from keeping tests stupid the most important tool to catch erroneous tests is to write and run them before implementing the passing code. Run Tests before Passing Them When tests are written before the required production code they should also be run before passing it. Doing so allows us to verify that the test fails for the expected reasons thus giving us a chance to catch errors in the test itself. Failing a test with an articulated expectation as to how and why the test should fail is in fact the most effective means with which we can fight buggy tests. Skipping this point we might move on to pass the test immediately. As soon as we have started writing production code we are a lot less likely to discover faulty testing logic and might as well end up passing the test thus sneaking the wrong behavior into production code without having tests that can tell us as much. Write Tests First To be able to run tests before passing them we obviously need to write them first as well. Because this book has given some insight into the test-driven development cycle and how it can apply to JavaScript the recommendation to write tests first should not come as a surprise. Writing tests upfront has benefits beyond making it easier to catch faulty tests. Tests first ensure that code is inherently testable. If you have ever attempted to retrofit unit tests onto code that was not originally written with testability in mind you will appreciate the importance of testable code. Writing testable code is .