Join live session: Test your components in browser with Storybook Test
Docs
Storybook Docs

Test runner

Storybook test runner turns all of your stories into executable tests. It is powered by Jest and Playwright.

These tests run in a live browser and can be executed via the command line or your CI server.

Setup

The test-runner is a standalone, framework-agnostic utility that runs parallel to your Storybook. You will need to take some additional steps to set it up properly. Detailed below is our recommendation to configure and execute it.

Run the following command to install it.

npm install @storybook/test-runner --save-dev

Update your package.json scripts and enable the test runner.

{
  "scripts": {
    "test-storybook": "test-storybook"
  }
}

Start your Storybook with:

npm run storybook

Storybook's test runner requires either a locally running Storybook instance or a published Storybook to run all the existing tests.

Finally, open a new terminal window and run the test-runner with:

npm run test-storybook

Configure

Test runner offers zero-config support for Storybook. However, you can run test-storybook --eject for more fine-grained control. It generates a test-runner-jest.config.js file at the root of your project, which you can modify. Additionally, you can extend the generated configuration file and provide testEnvironmentOptions as the test runner also uses jest-playwright under the hood.

CLI Options

The test-runner is powered by Jest and accepts a subset of its CLI options (for example, --watch, --maxWorkers). If you're already using any of those flags in your project, you should be able to migrate them into Storybook's test-runner without any issues. Listed below are all the available flags and examples of using them.

OptionsDescription
--helpOutput usage information
test-storybook --help
-s, --stories-jsonRun in stories json mode. Automatically detected (requires a compatible Storybook)
test-storybook --stories-json
--no-stories-jsonDisables stories json mode
test-storybook --no-stories-json
-c, --config-dir [dir-name]Directory where to load Storybook configurations from
test-storybook -c .storybook
--watchRun in watch mode
test-storybook --watch
--urlDefine the URL to run tests in. Useful for custom Storybook URLs
test-storybook --url http://the-storybook-url-here.com
--browsersDefine browsers to run tests in. One or multiple of: chromium, firefox, webkit
test-storybook --browsers firefox chromium
--maxWorkers [amount]Specifies the maximum number of workers the worker-pool will spawn for running tests
test-storybook --maxWorkers=2
--no-cacheDisable the cache
test-storybook --no-cache
--clearCacheDeletes the Jest cache directory and then exits without running tests
test-storybook --clearCache
--verboseDisplay individual test results with the test suite hierarchy
test-storybook --verbose
-u, --updateSnapshotUse this flag to re-record every snapshot that fails during this test run
test-storybook -u
--ejectCreates a local configuration file to override defaults of the test-runner
test-storybook --eject
--coverageRuns coverage tests on your stories and components
test-storybook --coverage
npm run test-storybook -- --watch

Run tests against a deployed Storybook

By default, the test-runner assumes that you're running it against a locally served Storybook on port 6006. If you want to define a target URL to run against deployed Storybooks, you can use the --url flag:

npm run test-storybook -- --url https://the-storybook-url-here.com

Alternatively, you can set the TARGET_URL environment variable and run the test-runner:

TARGET_URL=https://the-storybook-url-here.com yarn test-storybook

Set up CI to run tests

You can also configure the test-runner to run tests on a CI environment. Documented below are some recipes to help you get started.

Run against deployed Storybooks via Github Actions deployment

If you're publishing your Storybook with services such as Vercel or Netlify, they emit a deployment_status event in GitHub Actions. You can use it and set the deployment_status.target_url as the TARGET_URL environment variable. Here's how:

.github/workflows/storybook-tests.yml
name: Storybook Tests
on: deployment_status
jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    if: github.event.deployment_status.state == 'success'
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14.x'
      - name: Install dependencies
        run: yarn
      - name: Install Playwright
        run: npx playwright install --with-deps
      - name: Run Storybook tests
        run: yarn test-storybook
        env:
          TARGET_URL: '${{ github.event.deployment_status.target_url }}'

The published Storybook must be publicly available for this example to work. We recommend running the test server using the recipe below if it requires authentication.

Run against non-deployed Storybooks

You can use your CI provider (for example, GitHub Actions, GitLab Pipelines, CircleCI) to build and run the test runner against your built Storybook. Here's a recipe that relies on third-party libraries, that is to say, concurrently, http-server, and wait-on to build Storybook and run tests with the test-runner.

.github/workflows/storybook-tests.yml
name: 'Storybook Tests'
on: push
jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14.x'
      - name: Install dependencies
        run: yarn
      - name: Install Playwright
        run: npx playwright install --with-deps
      - name: Build Storybook
        run: yarn build-storybook --quiet
      - name: Serve Storybook and run tests
        run: |
          npx concurrently -k -s first -n "SB,TEST" -c "magenta,blue" \
            "npx http-server storybook-static --port 6006 --silent" \
            "npx wait-on tcp:6006 && yarn test-storybook"

By default Storybook outputs the build to the storybook-static directory. If you're using a different build directory, you'll need to adjust the recipe accordingly.

What's the difference between Chromatic and Test runner?

The test-runner is a generic testing tool that can run locally or on CI and be configured or extended to run all kinds of tests.

Chromatic is a cloud-based service that runs visual and interaction tests (and soon accessibility tests) without setting up the test runner. It also syncs with your Git provider and manages access control for private projects.

However, you might want to pair the test runner and Chromatic in some cases.

  • Use it locally and Chromatic on your CI.
  • Use Chromatic for visual and interaction tests and run other custom tests using the test runner.

Advanced configuration

Test hook API (experimental)

The test-runner renders a story and executes its play function if one exists. However, certain behaviors are impossible to achieve via the play function, which executes in the browser. For example, if you want the test-runner to take visual snapshots for you, this is possible via Playwright/Jest but must be executed in Node.

The test-runner exports test hooks that can be overridden globally to enable use cases like visual or DOM snapshots. These hooks give you access to the test lifecycle before and after the story is rendered. Listed below are the available hooks and an overview of how to use them.

HookDescription
setupExecutes once before all the tests run
setup() {}
preRenderExecutes before a story is rendered
async preRender(page, context) {}
postRenderExecutes after the story is rendered
async postRender(page, context) {}

These test hooks are experimental and may be subject to breaking changes. We encourage you to test as much as possible within the story's play function.

To enable the hooks API, you'll need to add a new configuration file inside your Storybook directory and set them up as follows:

.storybook/test-runner.ts
import type { TestRunnerConfig } from '@storybook/test-runner';
 
const config: TestRunnerConfig = {
  // Hook that is executed before the test runner starts running tests
  setup() {
    // Add your configuration here.
  },
  /* Hook to execute before a story is rendered.
   * The page argument is the Playwright's page object for the story.
   * The context argument is a Storybook object containing the story's id, title, and name.
   */
  async preRender(page, context) {
    // Add your configuration here.
  },
  /* Hook to execute after a story is rendered.
   * The page argument is the Playwright's page object for the story
   * The context argument is a Storybook object containing the story's id, title, and name.
   */
  async postRender(page, context) {
    // Add your configuration here.
  },
};
 
module.exports = config;

Except for the setup function, all other functions run asynchronously. Both preRender and postRender functions include two additional arguments, a Playwright page and a context object which contains the id, title, and the name of the story.

When the test-runner executes, your existing tests will go through the following lifecycle:

  • The setup function is executed before all the tests run.
  • The context object is generated containing the required information.
  • Playwright navigates to the story's page.
  • The preRender function is executed.
  • The story is rendered, and any existing play functions are executed.
  • The postRender function is executed.

Helpers

The test-runner exports a few helpers that can be used to make your tests more readable and maintainable by accessing Storybook's internals (e.g., args, parameters). Listed below are the available helpers and an overview of how to use them.

.storybook/test-runner.ts
import type { TestRunnerConfig } from '@storybook/test-runner';
 
import { getStoryContext } from '@storybook/test-runner';
 
const config: TestRunnerConfig = {
  // Hook that is executed before the test runner starts running tests
  setup() {
    // Add your configuration here.
  },
  /* Hook to execute before a story is rendered.
   * The page argument is the Playwright's page object for the story.
   * The context argument is a Storybook object containing the story's id, title, and name.
   */
  async preRender(page, context) {
    // Add your configuration here.
  },
  /* Hook to execute after a story is rendered.
   * The page argument is the Playwright's page object for the story
   * The context argument is a Storybook object containing the story's id, title, and name.
   */
  async postRender(page, context) {
    // Get the entire context of a story, including parameters, args, argTypes, etc.
    const storyContext = await getStoryContext(page, context);
 
    // Add your configuration here.
  },
};
 
module.exports = config;

Stories.json mode

The test-runner transforms your story files into tests when testing a local Storybook. For a remote Storybook, it uses the Storybook's stories.json file (a static index of all the stories) to run the tests.

Why?

Suppose you run into a situation where the local and remote Storybooks appear out of sync, or you might not even have access to the code. In that case, the stories.json file is guaranteed to be the most accurate representation of the deployed Storybook you are testing. To test a local Storybook using this feature, use the --stories-json flag as follows:

npm run test-storybook -- --stories-json

The stories.json mode is not compatible with watch mode.

If you need to disable it, use the --no-stories-json flag:

npm run test-storybook -- --no-stories-json

How do I check if my Storybook has a stories.json file?

Stories.json mode requires a stories.json file. Open a browser window and navigate to your deployed Storybook instance (for example, https://your-storybook-url-here.com/stories.json). You should see a JSON file that starts with a "v": 3 key, immediately followed by another key called "stories", which contains a map of story IDs to JSON objects. If that is the case, your Storybook supports stories.json mode.


Troubleshooting

The test runner seems flaky and keeps timing out

If your tests time out with the following message:

Timeout - Async callback was not invoked within the 15000 ms timeout specified by jest.setTimeout

It might be that Playwright couldn't handle testing the number of stories you have in your project. Perhaps you have a large number of stories, or your CI environment has a really low RAM configuration. In such cases, you should limit the number of workers that run in parallel by adjusting your command as follows:

{
  "scripts": {
    "test-storybook:ci": "yarn test-storybook --maxWorkers=2"
  }
}

The error output in the CLI is too short

By default, the test runner truncates error outputs at 1000 characters, and you can check the full output directly in Storybook in the browser. However, if you want to change that limit, you can do so by setting the DEBUG_PRINT_LIMIT environment variable to a number of your choosing, for example, DEBUG_PRINT_LIMIT=5000 yarn test-storybook.

Run the test runner in other CI environments

As the test runner is based on Playwright, you might need to use specific docker images or other configurations depending on your CI setup. In that case, you can refer to the Playwright CI docs for more information.

Learn about other UI tests