How I Test a Million UI States with Every Merge — Visual Testing with Storybook

Rate this content
Bookmark

Error state, loading state, awkward breakpoint, bad data, poor formatting, browser support. Every component can result represent hundreds or thousands of discrete visual states. How do you test it? Manually disable the network — temporarily. Insert bad code — just for a minute. Paw at the edge of your screen. Hack local database fixtures to bits. Frontend development has so many dimensions. Time and variation result in an infinite number of UI possibilities. In this talk, we'll use Storybook to progressively develop, test, and document our work — automating the grunt work of UI development.

This talk has been presented at React Advanced Conference 2022, check out the latest edition of this React Conference.

FAQ

The main goal of Michael Chan's presentation is to demonstrate that simple snapshots are not sufficient for testing UI and to introduce more robust tools for UI testing, particularly focusing on visual testing using Storybook with Chromatic.

Storybook is a tool for building UI components and pages in isolation, which helps in organizing and testing UI components. Chromatic is a cloud service that integrates with Storybook to provide visual testing capabilities. Together, they help in visually testing UI components by capturing snapshots and detecting changes, ensuring consistency across different environments.

The testing trophy in React is a concept that describes the proportion of different types of tests that should be written for effective testing. It emphasizes that integration tests should form the largest part of the testing strategy, which includes visually testing components to ensure they function correctly together.

The 'UI multiverse' described by Michael Chan refers to the complex, multi-dimensional challenges faced when building UIs. It encompasses dealing with multiple states like error, loading, and success, across various breakpoints, browser engines, and user abilities, resulting in a significant increase in the number of UI states that need to be managed and tested.

Component-driven development isolates UI components making them easier to manage and test. This approach allows for injecting state into components directly, enabling quicker and more focused testing, especially useful in visual testing where individual components can be evaluated in isolation.

Visual testing addresses the challenge of ensuring UI components look and function as expected across different states and environments. It is particularly useful for integrating components with browsers in isolation and testing them without the need for a full application stack, thus simplifying the testing process and reducing environment-related errors.

Storybook serves as a component catalog that not only showcases UI components but also provides interactive documentation. Features like auto-generated docs from type annotations and a playground for tweaking component props enhance understanding and allow non-developers to experiment with components, facilitating better testing and documentation.

Michael Chan
Michael Chan
29 min
21 Oct, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Talk discusses the need for more robust tools for visual testing in UI development. It explores the challenges in building UIs, including multiple views, variants, and breakpoints. The importance of component extraction and interactions is emphasized. The Talk also covers story derivation from components and interactions, UI testing with the test runner, and visual regression testing with Chromatic. Automating tests using GitHub Actions and common mistakes in using Storybook are discussed. The Talk concludes with a Q&A session.

1. Introduction to Visual Testing in UI Development

Short description:

My goal today is to convince you that just snapshots are not enough for what we're doing today and that we need more robust tools for testing our UI. We'll be talking about visual testing using Storybook with Chromatic, which are component-driven tools. The idea is to extract as much value as possible from one component.

What's up, everybody? How are you doing today? OK. It's OK. It's OK. I need more coffee. Man, my flight over here, I'm working on a couple hours of sleep, so I apologize if I things are a little bit rough today. But I'm super-excited to be here, super-excited to be hanging out with you. I'm a little bit worried about the timeline because like 20 minutes, I mean, like, I can talk about the subtle differences between number two pencils for 20 minutes. So I don't know how much of this we'll be able to thoroughly cover, but I'm excited to get through as much of it as we possibly can.

Now, my goal today is just to convince you that just snapshots are not enough for what we're doing today and that we need more robust tools for testing our UI. So we're gonna go through that. My name is Chan, Michael Chan, Chantastic, whatever you feel comfortable with. This is how you can find me online. Earlier this year, I gave a talk called Taming the UI Multiverse, which was a little bit more of like a feely, squishy version of this talk. We're gonna take a little excerpt of that and then jump into a little bit more code. But, if you want more stuff that's just talking about the feeling of UI development, how hard it is, and some of the challenges that we feel but don't really talk about a lot of times, this is gonna be a good follow-up talk for you.

So jumping right into what we're talking about today, who here is familiar with the testing trophy in React? Okay, only a handful. Okay, okay, hands are slowly starting to go up. So I think probably a good half of you. The idea behind the testing trophy is kind of a shape of how many tests we should write in any regard, and the biggest of those is in that integration slice of the pie. This is kind of a distribution of the types of tools that we use for the different types of testing. But integration is supposedly the biggest piece of that. Now I propose that there's this big piece of integration tests that we haven't actually integrated into our development workflows, which is visual testing. So that's what we're going to talk about today. And the tools that I've been using for this are Storybook with Chromatic. Those are the tools we're going to talk about. Now both of these tools kind of derive from this mindset of being component-driven. And so what's the idea of component-driven? How does it work with visual testing? Well, the idea behind it is really boils down to, how much value can we extract from one component? We have this really cool, isolated nugget of user interface. How can we, I guess to borrow a violent metaphor, how many birds can we kill with the stone of a component? Sorry, if you love birds. I love birds. I actually had a podcast for a little bit where we just reviewed bird songs.

2. Challenges in Building UIs

Short description:

Components allow you to inject a state and go fast. The UI multiverse is a multidimensional challenge in building UIs. Every UI has multiple views, including error, loading, and successful states. There are different variants and breakpoints to consider. Browser engines, usabilities, and device capabilities add complexity. Ballooning complexity includes authorization, props, state, and localization. Good documentation is essential.

A little different than a React podcast. One of the things that we haven't, components have really become every part of our life. But something we haven't explored a lot is how we integrate components with browsers in isolation. Up to this point, visual testing has often been bringing the full stack of our application into the browser, testing it. And that can be really hard to test, getting the environments right, getting all the personas right. Components allow you to just kind of like, inject a state and go really fast.

So, I want to describe the problem a little bit, and I call this the UI multiverse. And it's a description of the multidimensional challenge that we have when we're building UIs. And yes, like all of us, I was watching a lot of Marvel at the time that I was thinking through this. I also call this the 10-ish dimensions of web UI, or 35,000 perfect states. So, every UI starts with the thing that everyone thinks that they're building, the very clear vision of what we're going to make. But we all know as modern web developers that any view is potentially three views, an error state, a loading state, and then the successful loaded state. And then it goes even further, because like, we have like, different variants of those We could have a spinner, we could have a skeleton, the errors could be a 404, which is distinctly different from a 500. And then for our successful views, like, these multiply like rabbits, right? Because maybe we have 6 breakpoints or so that we actually think about and care about. But like, we could be supporting any number of breakpoints in reality. This gets a little bit more complicated multiplied by the number of browser engines that you have to support. Unfortunately, these are getting better year over year, but there are still subtle differences. Now, I would argue that the subtle differences aren't necessarily important to account for as long as you have a consistent experience inside of those browser engines. And then we have usabilities, right? So, are you using, like, touch, are you using sight, are you using your ears? In America, the CDC suggests that at least 4.6% of Americans have blindness or low vision. Now, it's really interesting because I've spent a huge amount of time testing and reworking views in Internet Explorer to save our app for, like, one and a half, 2% of users, but we dedicate so little time to the 4.6 plus percent of users who aren't able to use our sites with their eyes. And we also have, like, device capabilities. I'm not going to talk about that a lot, but it is part of the, like, dimensionality of UI development. And then these views get multiplied by all the complexity in our apps. So it could just be, like, authorization, that's one metric, but then you have, like, we all know how quickly props and state and the authorization of an application can really, like, balloon out of proportion. And if you have the privilege of having an app that's so popular that you need to localize it across different locales, well, now you have at least two versions that you have to worry about. So just take all of those views and multiply them by two. And if you've ever had to do this, you'll realize how poorly your apps are probably suited to just make that switch, because anytime you use a margin right for anything, like, all of that just is something you've got to fix now. And then docs. Docs are always a huge problem. We create these component libraries and we expect people to use them diligently, but we need to put in the work to make good documentation.

QnA