Chapter 3. Unit Testing and Refactoring

Tell me if this sounds familiar: We wanted to unit test, but it was hard to get the testing framework set up, and our JavaScript developers were also new to unit testing and struggled to write tests. Anyway, the deadline was hanging over us and there just wasn’t time to write unit tests. Besides, we can write the unit tests after we deploy to production!

If you’ve used one or more of these excuses, you aren’t alone. Unit testing is a skill that needs to be learned and practiced before it feels like a natural part of building software. Unit testing has to be learned through application. Reading books and watching videos is helpful, but a developer can only grow this skill through discovering what works and what doesn’t in writing tests for his or her code.

Unit Testing and Refactoring

If your application has little or no test coverage, refactoring is the perfect time to start introducing more unit tests. If you aren’t already practicing test-driven development, writing effective unit tests can be tough when you’re producing code under a deadline. As with unit tests, effective refactorings require patience and time. A project that’s designed for long-term success should factor in time for both activities.

Unit testing is a great way to concentrate and think about the changes needed to improve code. There is no perfect choice in refactoring code! But unit testing helps guide developers toward better choices by writing code as a set of inputs ...

Get Refactoring Angular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.