Storybook Component Test Runner (Experimental)
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
-
Error: Cannot find module 'test-runner-with-snap-speed-exp' from '<your-project-path>'
cause The package might not be installed, or there's a problem with module resolution (e.g., trying to `require` an ESM-only package in a CommonJS context).fixEnsure the package is installed: `npm install test-runner-with-snap-speed-exp`. If using `import`, ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
Error: Playwright browser did not launch correctly. Make sure you have installed browser binaries.
cause Playwright binaries are not installed or are corrupted, or a `playwright install` command was missed after installation.fixRun `npx playwright install` to download the necessary browser binaries. Ensure your environment has sufficient disk space and network access for the download. -
Error: Storybook URL 'http://localhost:6006' is not reachable or returned a non-200 status.
cause The Storybook instance is not running or is accessible on a different port/URL than configured in the test runner.fixVerify your Storybook server is running (`npm run storybook` or equivalent). Check the port and host. Update the `storybookUrl` in your `test-runner-with-snap-speed-exp` configuration to match the actual Storybook URL.
Warnings
- breaking As a `0.0.1` package, the API is highly unstable and subject to frequent breaking changes without prior notice. Expect `import` paths, function signatures, and configuration options to change between minor or even patch versions.
- gotcha This package relies heavily on external tools like Storybook and Playwright. Version incompatibilities between these dependencies can lead to unexpected failures, especially with Playwright's rapid release cycle.
- gotcha Performance (`snap-speed`) claims are likely experimental. Large Storybook setups or complex components might experience slow test execution or high resource consumption, negating the 'speed' aspect.
Install
-
npm install test-runner-with-snap-speed-exp -
yarn add test-runner-with-snap-speed-exp -
pnpm add test-runner-with-snap-speed-exp
Imports
- runStoriesTests
const runStoriesTests = require('test-runner-with-snap-speed-exp');import { runStoriesTests } from 'test-runner-with-snap-speed-exp'; - Config
import type { Config } from 'test-runner-with-snap-speed-exp'; - toHaveNoViolations
import { toHaveNoViolations } from 'test-runner-with-snap-speed-exp/matchers';
Quickstart
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);
});