Visual Testing with Storybook

Knowing your components look as intended in every permutation is not only a great way to test them visually, but also provides “living documentation” for them. This makes it easier for teams to:

  1. know what components are available to them in a given project and
  2. what props those components accept and what all of the states of that component are.

As your project grows over time having this information available will be invaluable. This is the function of the Storybook library. Storybook is a UI development environment for your UI components. With it, you can visualize different states of your UI components and develop them interactively.

Setting up your environment

Note that the following instructions are using npx. npx is a part of npm and in this case it allows you to automatically generate a file/folder structure complete with the default configuration. If you’re running an older version of npm (<5.2.0) you should run the following command instead: npm install -g @storybook/cli. You can then run sb init from your Gatsby root directory to initialize Storybook.

To set up Storybook you need to install dependencies and do some custom configuration. You can get started quickly by using the automated command line tool from your Gatsby root directory:

This command adds a set of boilerplate files for Storybook in your project. However, since this is for a Gatsby project, you need to update the default Storybook configuration a bit so you don’t get errors when trying to use Gatsby specific components inside of the stories.

Storybook version 5

Storybook version 5.3 brought a major change to how Storybook is configured.

When you first install Storybook the only configuration file that will exist is .storybook/main.js, which will have the default stories location and default addons.

Adjustments to Storybook’s default webpack configuration are required so that you can transpile Gatsby source files and to ensure you have the necessary Babel plugins to transpile Gatsby components. Add the following section to the module.exports object in .storybook/main.js.

The final output will look as follows:

Next create a new file under .storybook called preview.js. This configuration file preview.js is not responsible for loading any stories. Its main purpose is to add global parameters and decorators.

Add TypeScript Support

To configure TypeScript with Storybook and Gatsby, add the following configuration to .storybook/main.js.

Add tsx as a file type to look for in the stories array. This assumes the default Storybook path, but the array configuration can be modified to where your stories live.

Add the following code after the line containing config.resolve.mainFields = ["browser", "module", "main"]; (line 25) and before the return config; in the same function body (line 27):

The final output, with TypeScript support, will look as follows:

The babel-preset-react-app package will also need to be installed.

With setup completed for Storybook 5, you can continue with the information below to write stories.

Storybook version 4

To use Storybook version 4 with Gatsby, use the following setup instructions:

Once you have this configured you should run Storybook to ensure it can start up properly and you can see the default stories installed by the CLI. To run storybook:

Storybook CLI adds this command to your package.json for you so you shouldn’t have to anything other than run the command. If Storybook builds successfully you should be able to navigate to http://localhost:6006 and see the default stories supplied by the Storybook CLI.

However, if you use StaticQuery or useStaticQuery in your project Storybook needs to be run with the NODE_ENV set to production (as Storybook sets this by default to development). Otherwise babel-plugin-remove-graphql-queries won’t be run. Moreover Storybook needs to know about static files generated by Gatsby’s StaticQuery. Your scripts should look like:

Writing stories

A full guide to writing stories is beyond the scope of this guide, but we’ll take a look at creating a story.

First, create the story file. Storybook looks for all files with a .stories.js extension and loads them into Storybook for you. Generally you will want your stories near where the component is defined, however since this is Gatsby, if you want stories for your pages, you will have to create those files outside of the pages directory.

A good solution is to create a __stories__ directory next to your pages directory and put any page stories in there.

This is a very simple story without much going on, but honestly, nothing else really changes as related to Gatsby. If you want to learn more about how Storybook works and what you can do with it, check out some of the resources listed below.

Other resources