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.
npm install storybook-addon-test-codegenVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your Storybook installation is version 10.0.0 or higher when using storybook-addon-test-codegen v3.x. If on older Storybook, use compatible addon versions.
For Storybook v9.x, use storybook-addon-test-codegen v2.0.1. For older Storybook versions, consult the addon's compatibility table.
Always check the addon's README for the compatibility table. For Storybook 9.x use addon v2.0.1; for Storybook 8.3.x use v1.3.4; for Storybook 8.2.x use v1.0.3. For Storybook 10.x use v3.x.
In `.storybook/main.ts`, use `addons: ['storybook-addon-test-codegen']`. In `.storybook/preview.ts`, import `addonCodegen` and use `addons: [addonCodegen()]`.
Run `npm install --save-dev storybook-addon-test-codegen` or `npx storybook add storybook-addon-test-codegen`. Verify the addon name in `.storybook/main.ts`.
For CSF Next with `definePreview`, import `addonCodegen` and register it as `addons: [addonCodegen()]` in `.storybook/preview.ts`.
Ensure the 'Interaction Recorder' tab is active in the Storybook UI. Make sure your component elements are accessible and interactive according to standard web practices (e.g., proper roles, visible elements).
Add `import { userEvent, waitFor, within, expect } from "storybook/test";` at the top of your story file or test file where the `play` function is defined.