Storybook Addon Test Codegen

3.0.1 · active · verified Wed Apr 22

storybook-addon-test-codegen is a Storybook addon designed to streamline the creation of interactive tests for UI components. It enables developers to record interactions with their stories directly within the Storybook UI, automatically generating executable test code using utilities from `@storybook/test` (e.g., `userEvent`, `within`, `expect`). The current stable version is 3.0.1, with major version releases frequently aligning with Storybook's own major updates (e.g., v3.0.0 supports Storybook 10, v2.0.0 supports Storybook 9). This addon significantly reduces the manual effort of writing tests by converting recorded actions into functional test scripts and provides immediate feedback, including warnings for potential test improvements. Its core differentiator lies in its interactive, in-browser test generation capabilities within the Storybook development environment.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to install the addon, configure it in .storybook/main.ts, and shows an example Storybook story with a `play` function containing generated test code.

import type { StorybookConfig } from '@storybook/react-vite'; // Replace with your framework
import { userEvent, waitFor, within, expect } from "storybook/test";

// 1. Install the addon
// npx storybook add storybook-addon-test-codegen

// 2. Configure in .storybook/main.ts
const config: StorybookConfig = {
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  addons: ['storybook-addon-test-codegen'], // 👈 Register the addon here
  framework: {
    name: '@storybook/react-vite',
    options: {},
  },
  docs: {
    autodocs: 'tag',
  },
};
export default config;

// 3. Example Story with generated play function
// src/stories/Button.stories.ts (or .tsx)

export const MyButtonStory = {
  args: {
    label: 'Click Me',
  },
  play: async ({ canvasElement }) => {
    const canvas = within(canvasElement);
    const button = await canvas.findByRole('button', { name: 'Click Me' });
    await userEvent.click(button);
    await expect(button).toBeEnabled(); // Assertion generated by addon
  },
};

view raw JSON →