Structural Testing

Edit this page

For React, Jest’s snapshot testing is the best way to do Structural Testing. It’s painless to use and maintain. We’ve integrated Jest’s snapshot testing directly into Storybook using an addon called StoryShots. Now we can use existing stories as the input for snapshot testing.

What’s Snapshot Testing?

With Snapshot testing, we keep a file copy of the structure of UI components. Think of it like a set of HTML sources.

Then, after we’ve completed any UI changes, we compare new snapshots with the snapshots that we kept in the file.

If things are not the same, we can do two things:

  1. We can consider new snapshots that show the current state, and then update them as new snapshots.
  2. We can find the root cause for the change and fix our code.

We can also commit these snapshots directly into the source code.

Using StoryShots

StoryShots is our integration between Storybook and Jest Snapshot Testing.

To use StoryShots, first make sure you are inside a Storybook-enabled repo (make sure it has few stories).

Then, install StoryShots and any necessary framework-specific peer dependencies.

For instance if you’re testing a react project:

npm i -D @storybook/addon-storyshots react-test-renderer

Then, assuming you are using Jest for testing, you can create a test file storyshots.test.js that contains the following:

import initStoryshots from '@storybook/addon-storyshots';

initStoryshots({ /* configuration options */ });

Now you can snapshot test all of your stories with:

npm test

This will save the initial set of snapshots inside your Storybook config directory.

StoryShots First

After you complete any changes, you can run the test again and find all structural changes.

StoryShots Diff View

StoryShots also comes with a variety of customization options. Have a look at the StoryShots repo for more information.