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.

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

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Network Requests with Cypress
TestJS Summit 2021TestJS Summit 2021
33 min
Network Requests with Cypress
Top Content
Whether you're testing your UI or API, Cypress gives you all the tools needed to work with and manage network requests. This intermediate-level task demonstrates how to use the cy.request and cy.intercept commands to execute, spy on, and stub network requests while testing your application in the browser. Learn how the commands work as well as use cases for each, including best practices for testing and mocking your network requests.
Testing Pyramid Makes Little Sense, What We Can Use Instead
TestJS Summit 2021TestJS Summit 2021
38 min
Testing Pyramid Makes Little Sense, What We Can Use Instead
Top Content
Featured Video
Gleb Bahmutov
Roman Sandler
2 authors
The testing pyramid - the canonical shape of tests that defined what types of tests we need to write to make sure the app works - is ... obsolete. In this presentation, Roman Sandler and Gleb Bahmutov argue what the testing shape works better for today's web applications.
A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top Content
Let’s face it: technical debt is inevitable and rewriting your code every 6 months is not an option. Refactoring is a complex topic that doesn't have a one-size-fits-all solution. Frontend applications are particularly sensitive because of frequent requirements and user flows changes. New abstractions, updated patterns and cleaning up those old functions - it all sounds great on paper, but it often fails in practice: todos accumulate, tickets end up rotting in the backlog and legacy code crops up in every corner of your codebase. So a process of continuous refactoring is the only weapon you have against tech debt.In the past three years, I’ve been exploring different strategies and processes for refactoring code. In this talk I will describe the key components of a framework for tackling refactoring and I will share some of the learnings accumulated along the way. Hopefully, this will help you in your quest of improving the code quality of your codebases.

Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.
Testing Web Applications with Playwright
TestJS Summit 2022TestJS Summit 2022
20 min
Testing Web Applications with Playwright
Top Content
Testing is hard, testing takes time to learn and to write, and time is money. As developers we want to test. We know we should but we don't have time. So how can we get more developers to do testing? We can create better tools.Let me introduce you to Playwright - Reliable end-to-end cross browser testing for modern web apps, by Microsoft and fully open source. Playwright's codegen generates tests for you in JavaScript, TypeScript, Dot Net, Java or Python. Now you really have no excuses. It's time to play your tests wright.
Full-Circle Testing With Cypress
TestJS Summit 2022TestJS Summit 2022
27 min
Full-Circle Testing With Cypress
Top Content
Cypress has taken the world by storm by brining an easy to use tool for end to end testing. It’s capabilities have proven to be be useful for creating stable tests for frontend applications. But end to end testing is just a small part of testing efforts. What about your API? What about your components? Well, in my talk I would like to show you how we can start with end-to-end tests, go deeper with component testing and then move up to testing our API, circ

Workshops on related topic

Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
How to Start With Cypress
TestJS Summit 2022TestJS Summit 2022
146 min
How to Start With Cypress
Featured WorkshopFree
Filip Hric
Filip Hric
The web has evolved. Finally, testing has also. Cypress is a modern testing tool that answers the testing needs of modern web applications. It has been gaining a lot of traction in the last couple of years, gaining worldwide popularity. If you have been waiting to learn Cypress, wait no more! Filip Hric will guide you through the first steps on how to start using Cypress and set up a project on your own. The good news is, learning Cypress is incredibly easy. You'll write your first test in no time, and then you'll discover how to write a full end-to-end test for a modern web application. You'll learn the core concepts like retry-ability. Discover how to work and interact with your application and learn how to combine API and UI tests. Throughout this whole workshop, we will write code and do practical exercises. You will leave with a hands-on experience that you can translate to your own project.
Detox 101: How to write stable end-to-end tests for your React Native application
React Summit 2022React Summit 2022
117 min
Detox 101: How to write stable end-to-end tests for your React Native application
Top Content
WorkshopFree
Yevheniia Hlovatska
Yevheniia Hlovatska
Compared to unit testing, end-to-end testing aims to interact with your application just like a real user. And as we all know it can be pretty challenging. Especially when we talk about Mobile applications.
Tests rely on many conditions and are considered to be slow and flaky. On the other hand - end-to-end tests can give the greatest confidence that your app is working. And if done right - can become an amazing tool for boosting developer velocity.
Detox is a gray-box end-to-end testing framework for mobile apps. Developed by Wix to solve the problem of slowness and flakiness and used by React Native itself as its E2E testing tool.
Join me on this workshop to learn how to make your mobile end-to-end tests with Detox rock.
Prerequisites- iOS/Android: MacOS Catalina or newer- Android only: Linux- Install before the workshop
API Testing with Postman Workshop
TestJS Summit 2023TestJS Summit 2023
48 min
API Testing with Postman Workshop
Top Content
WorkshopFree
Pooja Mistry
Pooja Mistry
In the ever-evolving landscape of software development, ensuring the reliability and functionality of APIs has become paramount. "API Testing with Postman" is a comprehensive workshop designed to equip participants with the knowledge and skills needed to excel in API testing using Postman, a powerful tool widely adopted by professionals in the field. This workshop delves into the fundamentals of API testing, progresses to advanced testing techniques, and explores automation, performance testing, and multi-protocol support, providing attendees with a holistic understanding of API testing with Postman.
1. Welcome to Postman- Explaining the Postman User Interface (UI)2. Workspace and Collections Collaboration- Understanding Workspaces and their role in collaboration- Exploring the concept of Collections for organizing and executing API requests3. Introduction to API Testing- Covering the basics of API testing and its significance4. Variable Management- Managing environment, global, and collection variables- Utilizing scripting snippets for dynamic data5. Building Testing Workflows- Creating effective testing workflows for comprehensive testing- Utilizing the Collection Runner for test execution- Introduction to Postbot for automated testing6. Advanced Testing- Contract Testing for ensuring API contracts- Using Mock Servers for effective testing- Maximizing productivity with Collection/Workspace templates- Integration Testing and Regression Testing strategies7. Automation with Postman- Leveraging the Postman CLI for automation- Scheduled Runs for regular testing- Integrating Postman into CI/CD pipelines8. Performance Testing- Demonstrating performance testing capabilities (showing the desktop client)- Synchronizing tests with VS Code for streamlined development9. Exploring Advanced Features - Working with Multiple Protocols: GraphQL, gRPC, and more
Join us for this workshop to unlock the full potential of Postman for API testing, streamline your testing processes, and enhance the quality and reliability of your software. Whether you're a beginner or an experienced tester, this workshop will equip you with the skills needed to excel in API testing with Postman.
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
WorkshopFree
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.
Testing Web Applications Using Cypress
TestJS Summit - January, 2021TestJS Summit - January, 2021
173 min
Testing Web Applications Using Cypress
WorkshopFree
Gleb Bahmutov
Gleb Bahmutov
This workshop will teach you the basics of writing useful end-to-end tests using Cypress Test Runner.
We will cover writing tests, covering every application feature, structuring tests, intercepting network requests, and setting up the backend data.
Anyone who knows JavaScript programming language and has NPM installed would be able to follow along.