Skip to main content

Storybook Integration

Important

Access to this feature is currently limited to Enterprise customers as part of our commitment to providing tailored solutions. We are excited to announce that self-service access is under development and will be released shortly. Stay tuned!

An extension for Storybook's test-runner powered by Jest and Playwright to integrate effortless visual testing with Sauce Visual.

Introduction

This guide requires an existing Storybook setup.
You can alternatively take a look to our example repository.

All tests are run in a headless browser on your local machine/in your pipeline, so you don't need to worry about setting up a browser driver.

Compatibility

This package leverage's Storybook's test-runner and metadata generation system for enabling automatic testing of Storybook stories. We recommend running Storybook ^6.4.0 and an up-to-date version of the Storybook test-runner (>=0.13.0 at time of writing).

Getting Started

  1. Follow Storybook's instructions for setting up, installing, and configuring the test-runner if you haven't done so already. If you have already used or enabled the test-runner, you can skip this step.

  2. Install this plugin in an existing project from the root:

npm i --save-dev @saucelabs/visual-storybook
note

If you don't have Playwright as a dev dependency in your project, you'll need to install it as well:

npm i --save-dev playwright
  1. Eject your test-runner config and append the Sauce Visual storybook configuration:
note

If you already have a custom config file for the test-runner you can skip to the next step for appending our configuration bootstrap. If not see below or read through the storybook-runner docs to eject the default test configuration.

npx test-storybook --eject

The above should have created a file in the root directory, test-runner-jest.config.js. Edit this file and add an import for the sauce visual plugin:

const { getVisualTestConfig } = require('@saucelabs/visual-storybook');,

then append

...getVisualTestConfig(),

below the jest config spread. Your file should look similar to the example below:

const { getJestConfig } = require('@storybook/test-runner');
const { getVisualTestConfig } = require('@saucelabs/visual-storybook');

/**
* @type {import('@jest/types').Config.InitialOptions}
*/
module.exports = {
// The default configuration comes from @storybook/test-runner
...getJestConfig(),
// The configuration for Sauce Lab's Visual Integration
...getVisualTestConfig(),
/** Add your own overrides below
* @see https://jestjs.io/docs/configuration
* @see https://github.com/playwright-community/jest-playwright#configuration
*/
};
  1. Create a test-runner.js file in your storybook configuration directory (<root>/.storybook by default) if you do not already have one, and append our postRender hook into it. You can read more about this file in the hook API section. It should look something like below:
// .storybook/test-runner.js
const { postRender } = require('@saucelabs/visual-storybook');

module.exports = {
postRender,
};
  1. Sauce Visual relies on environment variables for authentications.
    Both SAUCE_USERNAME and SAUCE_ACCESS_KEY need to be set prior starting your Storybook Testrunner job.

Username and Access Key can be retrieved from https://app.saucelabs.com/user-settings.

export SAUCE_USERNAME=__YOUR_SAUCE_USER_NAME__
export SAUCE_ACCESS_KEY=__YOUR_SAUCE_ACCESS_KEY__
  1. Run your Storybook instance (or point to a built one), set the required environment variables (see here for all available / required fields), and run the test-runner! We'll take snapshots of all known Storybook stories and upload them into Sauce Visual.
# Spin up your storybook instance in a separate terminal window if you're not using a live /
# hosted one
npm run storybook

# Export your username and access key or set them in your CI variables. You can get these from
# app.saucelabs.com
export SAUCE_USERNAME=__YOUR_SAUCE_USER_NAME__
export SAUCE_ACCESS_KEY=__YOUR_SAUCE_ACCESS_KEY__

# Run the storybook command! Note: if you're running against a non-standard port or URL, you'll
# need to supply the --url parameter followed by the root url for your storybook instance. See the
# test-runner docs for more details / examples of config parameters.
# ex: --url http://localhost:6006
npx test-storybook
  1. Review your changes in the Sauce Visual Dashboard!

Customizing Your Builds (Environment Variables)

Below are the environment variables available in the visual-storybook plugin:

Variable NameDescription
SAUCE_USERNAMErequiredYour Sauce Labs username. You can get this from the header of app.saucelabs.com
SAUCE_ACCESS_KEYrequiredYour Sauce Labs access key. You can get this from the header of app.saucelabs.com
SAUCE_REGIONThe region you'd like to run your Visual tests in. Defaults to us-west-1 if not supplied. Can be one of the following:
'eu-central-1', 'us-west-1' or 'us-east-4'
SAUCE_BUILD_NAMEThe name you would like to appear in the Sauce Visual dashboard. Defaults to 'Storybook Build'
SAUCE_BRANCH_NAMEThe branch name or tag you would like to associate this build with. We recommend using your current VCS branch in CI.
SAUCE_PROJECT_NAMEThe label / project you would like to associated this build with.
SAUCE_VISUAL_BUILD_IDFor advanced users, a user-supplied SauceLabs Visual build ID. Can be used to create builds in advance using the GraphQL API. This can be used to parallelize tests with multiple browsers, shard, or more.
By default, this is not set and we create / finish a build during setup / teardown.
SAUCE_VISUAL_CUSTOM_IDFor advanced users, a user-supplied custom ID to identify this build. Can be used in CI to identify / check / re-check the status of a single build. Usage suggestions: CI pipeline ID.

Story / Global Configuration

Additional configuration options are exposed via the sauceVisual Storybook parameters. You can use these to tweak settings on a global, component, or per-story basis -- see the Storybook docs for details on how to apply them to each.

The below configuration options are also exported as the type SauceVisualParams from @saucelabs/visual-storybook if you'd like TypeScript types for them.

Parameters key: sauceVisual

KeyTypeDefaultDescription
clipbooleantrueIf the story & layout supports it, will automatically clip to the clipSelector to remove extraneous whitespace. Recommended to be used in conjunction with centered layout. Currently defaults to false, however, will default to true in a future version.
clipSelectorstring#storybook-rootThe selector to clip to when clip = true. Defaults to Storybook's default root element, #storybook-root.
delaynumber0 (no delay)A number, in ms, that we should delay the snapshot by. Useful if the beginning of the story has unavoidable / javascript animations.

Component-level Example:

const meta = {
title: 'Example/Button',
component: Button,
parameters: {
// ... Your other Storybook parameters here
sauceVisual: {
// Add storybook visual configuration options here
clip: true,
// clipSelector: '#custom-root-element',
// delay: 200,
},
},
};

export default meta;

Different Browsers and Devices

By default the tests are run on your local machine/in your pipeline with Chromium. You have the option to run them on different browser and device configurations preconfigured by playwright or define your own device, a combination or all of them. To do so, you need to add the following to your test-runner-jest.config.js file:

const { getJestConfig } = require('@storybook/test-runner');
const { getVisualTestConfig } = require('@saucelabs/visual-storybook');

/**
* @type {import('@jest/types').Config.InitialOptions}
*/
module.exports = {
// The default configuration comes from @storybook/test-runner
...getJestConfig(),
// The configuration for Sauce Lab's Visual Integration
...getVisualTestConfig(),
/** Add your own overrides below
* @see https://jestjs.io/docs/configuration
* @see https://github.com/playwright-community/jest-playwright#configuration
*/

// Add this to your config
testEnvironmentOptions: {
'jest-playwright': {
// With this option tests will be run against the defaultBrowserType of the devices, otherwise
// they run in a matrix against the default browser(s) configured below (chromium, if omitted)
useDefaultBrowserType: true,
// we still need to specify the browsers used by the devices
browsers: ['chromium', 'webkit', 'firefox'],
// this is actually the important part, we need to specify the devices we want to test against
devices: [ 'Desktop Edge', 'Desktop Firefox', 'Desktop Chrome', 'Desktop Safari', 'Pixel 5', 'iPhone 14 Pro Max']
},
};

If you'd like to configure your own devices, please follow the configuration steps inside the playwright docs.

Auto Testing Variations

Visual variant grid example

We have a separate Storybook plugin, @saucelabs/storybook-variants, available for rendering all variants of a component in a grid to ease the testing and development process. Read the full README on NPM for installation & usage.