Storybook

Visual testing for Storybook

👍

Covered in this doc

Installing @percy/storybook
Using @percy/storybook
How to configure @percy/storybook
Upgrading from v3 of @percy/storybook

📘

These docs apply to version v4.0.0 or later. Previous versions of @percy/storybook are incompatible with what is described in this document. v3.x docs can be found here

VersionVersion TestTest

Percy visual testing for Storybook.

Installation

$ npm install --save-dev @percy/cli @percy/storybook

Usage

Before running the following commands, make sure your project's PERCY_TOKEN is properly configured.

# Unix
$ export PERCY_TOKEN="<your-project-token>"

# Windows
$ set PERCY_TOKEN="<your-project-token>"

# Powershell
$ $Env:PERCY_TOKEN="<your-project-token>"

With a static Storybook build:

$ percy storybook ./storybook-build

With a local or live Storybook URL:

$ percy storybook http://localhost:9009
$ percy storybook https://storybook.foobar.com

Automatically run start-storybook:

$ percy storybook:start --port=9009 --static-dir=./public

CLI options:

-c, --config=config                              percy configuration file path
-d, --dry-run                                    prints a list of stories to snapshot without snapshotting
-i, --include=include                            pattern matching story names to only include for snapshotting
-e, --exclude=exclude                            pattern matching story names to always exclude from snapshotting
-h, --allowed-hostname=allowed-hostname          asset discovery allowed hostnames
-t, --network-idle-timeout=network-idle-timeout  asset discovery idle timeout
--disable-cache                                  disable asset discovery caches
-q, --quiet                                      log errors only
-v, --verbose                                    log everything
--silent                                         log nothing

Configuration

Storybook parameters are a set of static, named metadata about a story, used to control the behavior of Storybook features and addons. The percy parameter can be provided to add per-snapshot configuration options to a story or set of stories.

// individual stories
MyStory.parameters = {
  percy: { ... }
};
// global .storybook/preview.js
export const parameters = {
  percy: { ... }
};

The following percy Storybook parameters are accepted in addition to common per-snapshot options:

  • skip - Boolean indicating whether or not to skip this story.
  • name - Snapshot name. (default: ${story.kind}: ${story.name})
  • args - Story args to use when snapshotting.
  • queryParams - Query parameters to use when snapshotting.
  • waitForTimeout - Wait for a timeout before taking the snapshot.
  • waitForSelector - Wait for a selector to exist before taking the snapshot.
  • include - An array of regex patterns matching story names to only include for snapshotting.
  • exclude - An array of regex patterns matching story names to always exclude from snapshotting.
  • additionalSnapshots - An array of additional snapshots to take of this story.
    • prefix - A prefix added to this additional snapshot's name.
    • suffix - A suffix added to this additional snapshot's name.
    • name - Snapshot name. Replaces the inherited name.
    • args - Additional story args for this additional snapshot.
    • queryParams - Additional query parameters for this additional snapshot.
    • include - Only apply the additional snapshot to matching stories.
    • exclude - Do not apply the additional snapshot to matching stories.

Example config

  MyStory.parameters = {
    percy: {
      name: 'My snapshot',
      additionalSnapshots: [
        { prefix: '[Dark mode] ', args: { colorScheme: 'dark' } },
        { suffix: ' with a search', queryParams: { search: 'foobar' } }
      ]
    }
  };

With this example, 3 snapshots will be taken of this story with args and query params appended to the URL of each snapshot:

  # --dry-run will log snapshots without creating a new build
  # --verbose will show debug logs, including the snapshot url
  $ percy storybook --dry-run --verbose ./example-storybook
  # ...
  [percy] Snapshot found: My snapshot
  [percy] - url: [...]?id=component--my-story
  [percy] Snapshot found: [Dark mode] My snapshot
  [percy] - url: [...]?id=component--my-story&args=colorScheme:dark
  [percy] Snapshot found: My snapshot with a search
  [percy] - url: [...]?id=component--my-story&search=foobar

Percy config file options

In addition to common Percy config file options, this SDK also adds the following Storybook specific options:

# .percy.yml
version: 2
storybook:
  args: {}
  queryParams: {}
  waitForTimeout: 0
  waitForSelector: ''
  additionalSnapshots: []
  include: []
  exclude: []

See the configuration options above for details about each accepted config file option (note: the skip and name parameters are not accepted as Percy config file options).

Advanced

You can configure most Stoybook addons that use toolbars & globals via query params. For example, for an addon that accepts a global config option, you can set that query param like so:

  MyStory.parameters = {
    percy: {
      name: 'My snapshot',
      additionalSnapshots: [
        { suffix: 'globals example', queryParams: { globals: 'key:value' } }
      ]
    }
  };

Upgrading

Prior versions of the Storybook SDK were drastically different than the current version. The command, it's arguments, and how the SDK works internally have changed completely. Using the old command with new versions will now result in an error message. The new command is now integrated into @percy/cli as a plugin.

To use new versions of this SDK, you will have to also install the CLI with the SDK:

$ npm install --save-dev @percy/cli @percy/storybook

Since both the command and arguments have changed, you'll need to replace your existing usage with the new usage described above. For some projects, this may require setting additional configuration options. See the list of breaking changes below for details.

Breaking changes

Most importantly, the command itself has changed and all previous arguments are no longer accepted.

  • The percy-storybook command has been replaced with a percy CLI subcommand, percy storybook.

  • The previous --build_dir flag is now a command argument and there is no default build directory. If you relied on the default, it must now be explicitly provided.

    # before
    $ percy-storybook
    # after
    $ percy storybook ./storybook-static
    
    # before
    $ percy-storybook --build_dir ./build
    # after
    $ percy storybook ./build
    
  • The --widths flag is no longer accepted. Widths can be set using the respective widths Percy config file snapshot option or percy Storybook parameter.

  • The --minimum_height flag is no longer accepted and therefore no longer defaults to 800px. The default minimum height shared by all SDKs is 1024px. The minimum height can be set using the respective min-height Percy config file snapshot option or percy Storybook parameter.

  • The --debug flag is now --verbose, inherited from the CLI.

  • The --output_format flag is no longer accepted and has no alternative. If you relied on this flag, please open an issue.

  • The --rtl and --rtl_regex flags are no longer accepted. The --rtl flag duplicated stories and set the direction=rtl query parameter for the duplicate's URL. The --rtl_regex flag was used to determine when to create this RTL duplicate story.

    Click here to see how to replicate the old behaviour with new configuration options
    // .storybook/preview.js
    
    export const parameters = {
      percy: {
        // tell percy to take an additional RTL snapshot for matching stories
        additionalSnapshots: [{
          suffix: ' [RTL]',
          queryParams: { direction: 'rtl' },
          include: ['^FormElement: .*']
        }]
      }
    };
    

Performance

The old SDK did not take DOM snapshots or perform asset discovery, as all other modern Percy SDKs do. This sometimes resulted in flakey snapshots or snapshots with missing assets. However, DOM snapshots and asset discovery add an overhead cost of performance. Where the old SDK was very quick to simply upload the local build directory, the new SDK can be a little slower while it is capturing the real DOM and relevant assets of each story.

Unexpected diffs

Because the old SDK did not take DOM snapshots, JavaScript had to be enabled in our rendering environment for Storybook to properly load. This is in contrast to all of our other SDKs, where JavaScript is disabled by default to prevent flakey diffs caused by animations or other JavaScript running on the page. With the new SDK and real DOM snapshots, JS is disabled by default. If you upgrade and experience diffs due to the lack of JavaScript, it can be re-enabled using the matching Percy config file or per-snapshot option, enableJavaScript.