npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

storybook-addon-test-codegen

v3.0.1

Published

Addon for Storybook that generates test code for your stories.

Readme

Storybook Addon Test Codegen

NPM version NPM downloads NPM downloads GitHub license

Interact with your Storybook and get test code generated for you. To see this live, check out the demo.

Alt Text

Installation

First, install the package using Storybook's commands:

npx storybook add storybook-addon-test-codegen

Storybook version compatibility

This addon requires Storybook version 10.0.0 or higher.

For older versions of Storybook, use npm directly to install the following addon versions:

| Storybook Version | Addon Version | |-------------------|---------------| | 9.* | 2.0.1 | | 8.3.* | 1.3.4 | | 8.2.* | 1.0.3 |

Manual installation

The npx storybook add command should automatically do everything that is necessary to set up this addon. If for any reason you need to do the steps manually, here's what you have to do.

Install the addon:

npm install --save-dev storybook-addon-test-codegen

Once installed, register it as an addon in .storybook/main.ts.

// .storybook/main.ts

// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type {StorybookConfig} from '@storybook/your-framework';

const config: StorybookConfig = {
  // ...rest of config
  addons: [
    'storybook-addon-test-codegen', // 👈 register the addon here
  ],
};

export default config;

Usage

Enable recording in the Interaction Recorder tab in the Storybook UI. Interact with your components as you normally would, and the addon will generate test code for you. Click "Add assertion" to add assertions like expect().toBeVisible() to the generated code.

Click on "Save to story" to save the generated code to the story file. Done 🎉

Alternatively, copy both imports and the generated code to your test file like so:

// MyComponent.stories.tsx

// 👇 Add the generated imports here
import {userEvent, waitFor, within, expect} from "storybook/test";

export const MyComponent = {
  // ...rest of the story

  // 👇 Add the generated test code here
  play: async ({canvasElement}) => {
    const canvas = within(canvasElement.ownerDocument.body);
    await userEvent.click(await canvas.findByRole('textbox', {name: 'Name'}));
    await userEvent.type(await canvas.findByRole('textbox', {name: 'Name'}), 'John Doe');
}
}

Usage with CSF Next

If you're using CSF Next syntax with definePreview and defineMain, you'll need to register the addon in two places. This is done automatically by the Storybook add command. If you need to do it manually, change the following files:

  1. The .storybook/main.ts file:
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import {defineMain} from '@storybook/your-framework/node';
export default defineMain({
	// ...
	addons: ['storybook-addon-codegen'],
});
  1. The .storybook/preview.ts file:
import addonCodegen from 'storybook-addon-codegen';
import {definePreview} from '@storybook/react-vite';
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import {definePreview} from '@storybook/your-framework';

export default definePreview({
	// ...
	addons: [addonCodegen()],
});

Warnings

The addon helps you write better tests by showing warnings when it detects potential improvements in your selectors. It highlights cases where:

  • Roles are used without names (which can make tests fragile)
  • Query selectors are used (which can break when HTML structure changes)
  • Test IDs are used (which don't reflect how users interact with your app)

For each warning, you'll get suggestions on how to improve your selectors to make tests more reliable and maintainable.

Warnings in action

API

Custom test-id selector

To set a custom data-test attribute to use for generating findByTestId queries, add the following to your preview.ts:

import {configure} from 'storybook/test';

configure({
  testIdAttribute: 'my-custom-attribute',
});

Contributing

Any contributions are welcome. Feel free to open an issue or a pull request.

Development Workflow

When developing this addon locally, there are two different workflows you can use:

Recommended: Separate watch and serve (saving to file works)

Run these two commands in separate terminals:

pnpm start        # Watch addon files and rebuild on changes
pnpm storybook    # Run Storybook (not in watch mode)

Note: With this setup, the "Save to story" feature works correctly. However, when you make changes to the addon itself, you'll need to manually restart pnpm storybook to pick up the new addon files.

Alternative: Full watch mode (saving to file doesn't work)

pnpm start:storybook  # Runs both addon and Storybook in watch mode

This command runs both the addon build (tsup) and Storybook in watch mode, so changes to the addon are automatically picked up by Storybook without manual restarts. However, the "Save to story" feature has historically not worked with this setup and we haven't been able to resolve this issue yet.