Test your UI in the REAL Browser

Rate this content
Bookmark

Imagine writing a complex function without unit tests. You would have to verify every scenario manually, over and over again. Cumbersome, but that's how most teams build user interfaces.


Imagine if you could build UIs and test UIs in the same place. If your components included expectations for how they were supposed to behave, you'd know the instant they broke.

Storybook provides an organized approach to building UIs. You document a component's use-cases as stories, which are then rendered in isolation. Stories are like tests, but for UI. Storybook interaction testing allows you to script interactions and check expectations in the story itself. That allows you to run and debug UI tests in the same environment UI components are developed for: your browser.

This talk has been presented at TestJS Summit 2021, check out the latest edition of this JavaScript Conference.

FAQ

Storybook is a tool for building UI components in isolation from the main application, allowing developers to create and test individual components with various states and variations. It supports all major frontend frameworks and can be extended with hundreds of add-ons. Storybook helps organize components into a searchable library, making it easier for developers to reuse components and ensure consistency across projects.

Component-driven development enhances efficiency by allowing developers to reuse existing components instead of creating custom UI pieces from scratch. It facilitates parallel work across different team members or teams, speeding up the development process. Additionally, focusing on component APIs promotes more durable and maintainable user interfaces.

In Storybook, component variations are managed through 'Stories'. Each Story represents a version of a component under specific conditions, such as loading states, error states, and various user contexts like language or theme preferences. This approach allows developers to visualize and test how components behave under different scenarios.

Storybook addresses challenges such as the slow and flaky nature of testing against a running app, difficulty in reproducing errors, and the complexity of testing numerous scenarios manually. It provides a controlled environment to focus on the appearance and functionality of components, simplifying the testing process.

Yes, Storybook can be extended with hundreds of add-ons to integrate with various design tools like Figma, connect components to data sources, simulate how components are perceived by colorblind users, generate documentation, and more. This integration helps streamline the workflow from design to development to testing.

Interactive stories in Storybook allow developers to define interactions within the stories through a 'play' function, which runs after the component renders. This feature enables the simulation of user behaviors and interaction testing within the browser, enhancing the capability to test complex UI components and interactions more effectively.

Storybook supports collaborative development by allowing teams to build a shared component library that is organized and searchable. Developers can upload a static version of their Storybook to share with colleagues or clients, facilitating review and feedback. This collaborative environment helps ensure consistency and quality across the development process.

Future developments for Storybook include tools for making assertions in play functions with the Interactions add-on, enhancing the ability to debug tests interactively in the browser. Additionally, integration with more testing libraries and the expansion into app development sectors are expected to drive growth and increase adoption.

Gert Hengeveld
Gert Hengeveld
33 min
19 Nov, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Storybook is a powerful tool for building UI components and testing them. It allows for easy reuse and compatibility with other tools. Storybook 6.4 introduces interactive stories and live coding, making it easier to create and debug complex components. It also integrates with popular testing libraries like Jest and Testing Library. Storybook aims to bridge the gap between end-to-end testing and unit testing, providing automated testing options for UI components.

1. Introduction to Gert Hengeveld and Storybook

Short description:

Hi, I'm Gert Hengeveld, a full stack software engineer at Chromatic. Today I'm excited to show you something I've been working on for the past couple of months. User interfaces are built using components, and component-driven development brings a lot of benefits. Testing UIs is still hard to do right, and existing test tools leave a lot to be desired. Storybook is a tool to build UI components, providing a clean room environment and working with all major frontend frameworks.

Hi, I'm Gert Hengeveld, a full stack software engineer at Chromatic. Chromatic was founded by Storybook maintainers and provides a platform for visual regression testing and review on top of it. I work from my home in The Netherlands, and because Storybook is open source, a lot of my work is out there in the open. Of course, you can follow me on Twitter for the occasional updates on UI development and best practices.

Today I'm excited to show you something I've been working on for the past couple of months. But first, let's cover the basics. These days, user interfaces are built using components. All modern frameworks provide a way to do component-driven development. Even server frameworks like Ruby on Rails now provide a way to build and use components. This makes sense because component-driven development brings a lot of benefits. You can work more efficiently because not every piece of UI has to be custom-made. You can speed up development by parallelizing work across people and teams. And user interfaces become more durable as you put more thought into component APIs by working on them in isolation.

Despite efforts by framework authors to lower the barrier to creating great user interfaces, testing them is still hard to do right. Testing against a running app can be slow and flaky, and reproducing an error is hard. Complex business logic means there are tons of scenarios and states to consider which you can't possibly test manually. Especially when your UI is in constant development. Existing test tools also leave a lot to be desired. If you think about it, it's silly how the status quo is to run unit tests with JS DOM, which is a totally different environment than the one your user interface will end up running in. Or that you have to install a separate browser to run and debug your interaction tests.

In case you're not familiar with Storybook yet, it's a tool to build UI components. It provides a clean room environment for your components, so you can focus on the way they work, and look in all of their states and variants. It works with all major frontend frameworks. Storybook is primarily built to run on your local machine, alongside your project code, as you develop or test applications. But you can also upload a static version of it to share with colleagues or customers. Storybook can be extended with hundreds of add-ons, to integrate with design tools like Figma or hook up components to a data source, render components like a colorblind person would see them, or generate component documentation. It's the single source of truth for UI components. When you work in Storybook, you'll be building components in isolation. Storybook provides a canvas on which it will render your components. In a typical workflow, you'll run your Storybook alongside your code editor.

2. Introduction to Storybook and Component Stories

Short description:

Storybook is a powerful tool for cataloging and categorizing component variants. It's trusted by thousands of development teams and used at companies like Shopify, GitHub, IBM, and the BBC. Stories are defined using component story format (CSF), allowing for easy reuse and compatibility with other tools. Now let's dive into some code with a simple Stories file for a Badge component.

Storybook is all about cataloging component variants, and categorizing components to form an organized, searchable component library. This will help everyone find the components they need, making it easier to reuse what's already there.

Storybook is an open source project powered by a large community of contributors. And it's trusted by thousands of development teams around the world. It's used at companies like Shopify, GitHub, IBM, and the BBC. It's even popping up on people's resumes more and more!

Storybook's main innovation is the concept of Stories. Most UI components have more than one variation. A Story is a piece of code that renders a component in one such variation. Think of things like loading states, error states, and functional states, such as a toggle. But there's also edge cases, like extreme values, overflowing content, or missing data. And finally, there's context-dependent variations, such as whether a user is signed in or not, their language, whether they prefer a dark theme, or what their assigned variant is in an A-B test. And to make matters worse, there's also countless possible combinations, certainly not something you'd want to test manually.

Stories are defined using component story format, or CSF for short. It's an open specification supported not just by Storybook, but by many other tools. This means you can write stories once and reuse them in various places. There's no API lock-in, because you're simply writing vanilla JavaScript. You can totally use stories with Playwright or Cypress if you want.

Now let's dive into some code. Here's what a simple Stories file for a Badge component looks like. At the top, we import the component and define a metadata object. This is the default export. This object can be used to define things like the type of data this component needs, where it goes in the component hierarchy, specify default arguments, and wrap the component with a data provider. In this case, we only need to tell Storybook which component we're rendering. And we're also specifying a default label. The badge component has a small and a large variant, so we define two stories, small and large, which only differ in their size property. To write a story, really all it takes is a plain JavaScript object. Now that you know how to define stories, we can add interactions. Sometimes a component has variants which cannot be expressed by passing arguments alone. Maybe you can't or don't want to change the API of the component, or the component is nested inside of another component, such as a page. Either way, passing args may not be an option.

QnA