Component Testing for Engineering Leadership

June 6, 2023

By Christopher Baus

This post is the third of a series that explores how Component Testing can benefit different parts of your team or organization. If you’re wondering why you and your team should adopt Component Testing, this series is for you!

We covered how developers and QA teams can improve the quality of their front-end code through Cypress Component Testing. In this post, we’ll dig into how Component Testing can improve the efficiency of the managers’ teams.

As an engineering leader, you are often challenged to deliver higher quality applications with fewer resources. Attempting to ensure the quality of components with a complex array of common tools can require a significant amount of time, and ultimately slow release cadences and increase the overall development cycle.

Cypress Component Testing is a new type of testing framework that allows engineering teams to  build high quality, durable web interfaces with component frameworks. Using Cypress Component Testing to test the behavior of components in isolation, in the browser, with the standard Cypress API, can boost the efficiency of your teams.

In a previous blog entry, I discussed the reasons I joined Cypress to work on Component Testing. I found existing component testing methods to be complex and often didn’t test the core functionality of components. I saw a better way forward in Cypress Component Testing.

From a manager’s perspective, Cypress Component Testing:

  1. Reduces the need to onboard and train engineers on multiple testing APIs
  2. Encourages eEngineers to focus on testing the behavior of components, not the internals
  3. Enables engineers to use test-driven development (TDD) methodologies when writing tests
  4. Reduces the dependence on End-to-End (E2E) tests to validate an application

What teams is component testing suitable for?

While it is possible for quality teams outside product development to create and maintain component tests, component tests are commonly written by the engineering teams who create the components.

Cypress Component Testing enables the testing of UI components built with popular component frameworks like React, Vue, and Angular in isolation in the browser, using the Cypress API.

E2E testing, on the other hand, does not require internal knowledge about the application, and is often handled by separate quality teams outside the product development team.

Conversely, developing component tests requires access to the component source code. Components can be mounted and tested in the browser, allowing tests to be developed in conjunction with the UI components. This makes component testing an ideal match for test-driven development.

Develop tests with your code

Using a component framework to develop your applications represents a paradigm shift in application development. By developing your application as independent components, they can be designed and tested in isolation, and later assembled into the final application.

Following the General Availability release of Cypress Component Testing, developers can mount and render components in their browser in the Cypress app. They can also generate specs from component files and run Cypress component tests. This allows components to be developed iteratively with quick feedback using the native browser.  

Quick iterations streamline both development and feedback.

Cypress as the universal testing API

As engineering leaders, we are constantly challenged with increasing efficiency and doing more with less. The cost of context switching can significantly impact team productivity, whether it involves switching to a different task or even a different API. Cypress Component Testing uses the existing Cypress API for everything except actually mounting a component. This allows engineers to create and test components in isolation, and move seamlessly to end-to-end testing when the component is complete. This allows Cypress to act as a universal testing API and effectively reduce onboarding time for new engineers joining a team, enabling them to become productive quickly.

Focus on behavior and not implementation

One of the potential pitfalls of using browserless testing frameworks to test components is that such frameworks often rely on the framework internals or tightly couple the tests to specific implementation aspects (such as function names). Because Cypress CT focuses on testing the behavior and not the implementation of components, it becomes easier to ensure correctness following a code refactor.

When developers focus on functionality rather than implementation details, they write tests that verify the behavior of individual components without being tied to their implementation. This means that the tests will still pass even if the implementation of the component changes, as long as the functionality remains the same. This makes it easier for developers to refactor a component’s code without having to rewrite its tests.

Conversely, browserless testing environments pose challenges to this user-focused testing approach. The test writer must manually manipulate the simulated DOM to various states in order to make assertions and handle many tasks that typically "just work" in a browser.

For example, suppose a developer wants to refactor a component by changing its internal implementation, such as updating function names or code structure. If the tests for the component are tightly coupled to the implementation details, such as by relying on specific methods or computed properties, then the tests will likely break after the refactor. The developer would then need to spend time updating the tests to reflect the new implementation, which can be time-consuming and error-prone. It becomes difficult to have confidence that your code behaves the same if the tests require extensive modifications.

However, if the developer has written Cypress CT tests that focus on the functionality of the component, rather than its implementation details, then the tests will continue to pass after the refactor as long as the functionality remains unchanged. This makes the code base more malleable, making it easier for teams to refactor the code without having to rewrite tests.

Speed development by reducing dependency on E2E testing

Component tests focus on testing individual components of an application in isolation, while Cypress E2E tests test the application as a whole, including all its components and their interactions. Due to the narrower scope, Cypress component tests require less setup and are easier to write, debug, and maintain.

Component tests can be executed without relying on external dependencies or services, while Cypress E2E tests usually require the application to be deployed and all its dependencies to be up and running. This makes Cypress component tests faster to execute and easier to set up and maintain.

Because component tests don’t encompass testing the entire application and its interactions, they execute more quickly. This means that developers can quickly run component tests locally during development, without having to wait for a full application deployment.

Component tests run in isolation and do not depend on the state of other components or services in the application, which makes them less prone to flakiness or false negatives. On the other hand, because they cover more ground, E2E tests can be affected by changes in other parts of the application or external services, which can lead to false positives or false negatives.

When a component test fails, it is usually easier to identify the problem because it is confined to a specific component. In contrast, when a E2E test fails, it can be more challenging to pinpoint the issue, especially if it involves the interaction between multiple components or services.

In summary, component tests are faster to develop and deploy than E2E tests because of their smaller scope, fewer dependencies, faster execution, better isolation, and easier debugging. However, it's worth noting that E2E tests are still important for testing the complete functionality of an application and integration of an application.

Conclusion

Leaders are constantly challenged to increase the efficiency of their teams, and Cypress Component Testing is a powerful tool which can help leaders in achieving their objectives. By enabling developers to quickly and easily test individual components of their applications, Cypress Component Testing helps detect bugs and errors early in the development process. This, in turn, reduces the need for costly and time-consuming debugging later on.

Additionally, the ability to focus on testing the behavior of components in isolation using the standard Cypress API allows for identification of  areas for improvement, leading to faster and more efficient development overall. Overall, Cypress Component Testing can help maximize your team's productivity and success.

Check out our docs to write your first component test in 5 minutes or less. As always, if this feature is helpful or if you have other ideas or feedback for our team, let us know on Github.

To learn more about Component Testing for Engineering Leadership, and the other implications of Component Testing for your organization, join us for a Cypress Community Event, Getting Started with Component Testing, on June 27, 2023.