Writing Good Tests
for Vue Applications
A Book by Markus Oberlehner
This book is for Vue developers who want to up their testing game and write good tests for Vue applications. Maybe, like me, you already wrote your fair share of bad tests and wonder how to reach the promised land of fast feedback loops, rapid release cycles, and refactoring with confidence testing advocates keep talking about. In this book, we'll explore how!
What's in the Book
If your primary goal is to learn just the basics of how to use tools like Vue Test Utils or Jest, this is not the perfect book for you. Whereas if, instead, you want to know the secrets of how to write tests that enable rapid feature development and refactoring with confidence while introducing as few bugs as possible, look no further!
Breakdown of Testing Approaches
We will explore different testing approaches, from manual testing to automated End-to-End (E2E) System, Application, Component, and Unit Tests. Each technique has unique advantages and limitations, and understanding their nuances will enable us to select the most suitable methods for our specific project requirements.
Planning Our Testing Strategy
After analyzing the various testing approaches, it's crucial to emphasize that different tests come with varying costs. Therefore, we must adapt and balance expensive (manual and E2E System Tests) and more affordable tests (Application, Component, and Unit Tests) to optimize our testing strategy.
The Three Principles for Writing Good Tests
The more flexible our code is, the easier and quicker we can react to new requirements. So, especially in projects we maintain and grow over many years (sometimes even decades), it is essential to improve the code and adapt it to new realities continuously. To do this, we need to write tests that adhere to The Three Principles for Writing Good Tests.
Setting Up the Perfect Test Environment
At this point, we have the necessary theoretical knowledge to start writing good tests. Now, it's time for practice! However, before we can start writing tests, we need to sharpen our tools. So this chapter is about how to put together our perfect test setup.
Designing Effective Application Tests
We'll discuss the fundamentals of Application Testing, for example, why it is important to test every new feature and where we draw the line between what qualifies to be part of our application and what is not. We'll then delve into Domain-specific Languages (DSL), exploring how a custom DSL can make our tests more resilient and readable. By the end of this chapter, you'll have a solid understanding of how to design effective Application Tests that are robust, efficient, and aligned with the requirements of your business domain.
Mastering Component Testing
In this chapter, we will explore Component Testing in depth. We'll take a closer look at the role of TypeScript in ensuring the correctness of our application, how we can craft high-quality Component Tests, and when it is even appropriate to write Component Tests in addition to Application Tests. These insights will help you leverage the power of Component Tests to create a more robust and maintainable application, ensuring that your components work as intended and interact seamlessly.
Unit Testing Fundamentals
Unit tests are the perfect place for testing edge cases and multiple variations as well as complicated operations. For example, suppose we have a function for formatting prices. We might test what happens if we need to round the price to the next cent. Or that we add commas and decimal points correctly. We don't want to test all possible formatting variations in dedicated acceptance tests, but we can easily do this in a unit test.
Testing in the Agile World
I have often experienced that many people perceive automated testing as an additional step—even worse: a further step at the end of the process. As a result, we do not benefit from the many advantages that automated testing offers us. On the contrary, tests performed at the end of the process tend to slow us down. Slower progress leads to more and more stakeholders questioning the merits of automated testing in general. Therefore, it is crucial to integrate testing into our process in a way that enables us to reap all the benefits and minimize the disadvantages. Only then will we have long-term success with our testing strategy.
Diving into Test-Driven Development (TDD)
By embracing TDD, we become part of an elite group of developers prioritizing a software project's code quality, maintainability, and long-term success. TDD empowers us to create robust applications that stand the test of time. This chapter will walk you through writing code using the Test-Driven Development (TDD) approach.