Storybook Component Test Runner (Experimental)

0.0.1 · active · verified Sun Apr 19

The `test-runner-with-snap-speed-exp` package is an experimental, early-stage test runner specifically designed for Storybook stories. Currently at version `0.0.1`, it aims to provide a fast and integrated testing experience for UI components, likely leveraging Playwright for browser automation and potentially integrating with Jest and Testing Library for assertions and component interactions. Its core utility is to allow developers to write tests that directly target Storybook stories, facilitating visual and functional regression testing within the Storybook ecosystem. Given its `0.0.1` version, it is under active development, but a stable release cadence is not yet established, and API changes are highly probable. Key differentiators include its tight coupling with Storybook for isolated component testing and a focus on testing speed.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure and run tests against a Storybook instance using the `test-runner-with-snap-speed-exp`. It sets up a basic Playwright environment, navigates to individual stories, and performs a visual snapshot test for each. It's designed to be run as part of a CI/CD pipeline or local development process.

import { runStoriesTests } from 'test-runner-with-snap-speed-exp';
import { expect } from '@playwright/test';

// Assuming a Storybook instance is running at http://localhost:6006
const storybookUrl = process.env.STORYBOOK_URL ?? 'http://localhost:6006';

const config = {
  storybookUrl: storybookUrl,
  /**
   * Function to execute before all stories are loaded.
   * Useful for global setup like custom Playwright setup or mocking.
   */
  setup: async ({ page }) => {
    await page.goto(storybookUrl);
    console.log(`Navigated to Storybook at ${storybookUrl}`);
  },
  testMatch: ['**/*.stories.ts?(x)', '**/*.stories.js?(x)'],
  /**
   * Define custom test logic for each story.
   * This example performs a basic snapshot comparison.
   */
  test: async ({ page, story, test }) => {
    test(`Story: ${story.title} - ${story.name}`, async () => {
      await page.goto(story.url);
      // Wait for the story to fully render and stabilize
      await page.waitForSelector('#root div');

      // Simple visual snapshot test using Playwright's API
      // In a real scenario, a more robust snapshot library would be integrated.
      const screenshotPath = `screenshots/${story.title.replace(/ /g, '_')}-${story.name.replace(/ /g, '_')}.png`;
      await expect(page).toHaveScreenshot(screenshotPath, { maxDiffPixelRatio: 0.05 });
      console.log(`Snapshot taken for ${story.title}/${story.name}`);
    });
  },
};

// Execute the test runner
runStoriesTests(config).catch(error => {
  console.error('Test runner failed:', error);
  process.exit(1);
});

view raw JSON →