Detalhes do pacote

storybook-addon-test-codegen

igrlk15.2kMIT2.0.1

Addon for Storybook that generates test code for your stories.

storybook-addons, interactions, test, codegen

readme (leia-me)

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.

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

Storybook version compatibility

This addon requires Storybook version 9.0.0 or higher.

For older versions of Storybook, use the following addon versions:

Storybook Version Addon Version
8.3.* 1.3.4
8.2.* 1.0.3

Register the Addon

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

// .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-essentials',
    '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');
  }
}

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.

changelog (log de mudanças)

v2.0.1 (Wed Jun 04 2025)

🐛 Bug Fix

  • fix: fixes an issue where using getAbsolutePath would make the addon not work #35 (@Gawdfrey)

Authors: 1


v2.0.0 (Fri May 30 2025)

💥 Breaking Change

📝 Documentation

  • update readme with version compatibility table #33 (@igrlk)

Authors: 2


v1.3.4 (Mon May 12 2025)

🐛 Bug Fix

  • always allow to click on assertion menu #32 (@igrlk)

Authors: 1


v1.3.3 (Sun Apr 20 2025)

🐛 Bug Fix

Authors: 1


v1.3.2 (Fri Apr 18 2025)

🐛 Bug Fix

Authors: 1


v1.3.1 (Wed Apr 16 2025)

🐛 Bug Fix

  • turn off recording when saving story / navigating to other story #26 (@igrlk)

Authors: 1


v1.3.0 (Mon Apr 14 2025)

🚀 Enhancement

  • Display element selector warnings #24 (@igrlk)

🧪 Tests

Authors: 1


v1.2.0 (Sun Mar 23 2025)

🚀 Enhancement

Authors: 1


v1.1.1 (Tue Mar 11 2025)

🐛 Bug Fix

  • support for react@19 & non-react packages #20 (@igrlk)

Authors: 1


v1.1.0 (Mon Mar 03 2025)

🚀 Enhancement

🐛 Bug Fix

  • add workflow_dispatch and pull_request to deploy-storybook #17 (@igrlk)

⚠️ Pushed to main

Authors: 1


v1.0.3 (Sat Feb 15 2025)

🐛 Bug Fix

Authors: 1


v1.0.2 (Wed Feb 12 2025)

🐛 Bug Fix

  • don't type-cast if story is in javascript #15 (@igrlk)

Authors: 1


v1.0.1 (Tue Feb 11 2025)

🐛 Bug Fix

  • bump peer dependency requirement #14 (@igrlk)

Authors: 1


v1.0.0 (Wed Feb 05 2025)

💥 Breaking Change

Authors: 1


v0.0.16 (Wed Jan 29 2025)

🐛 Bug Fix

Authors: 1


v0.0.15 (Sat Jan 25 2025)

🐛 Bug Fix

📝 Documentation

Authors: 1


v0.0.14 (Sun Jan 12 2025)

🐛 Bug Fix

  • add missing package.json fields #8 (@igrlk)

📝 Documentation

Authors: 1


v0.0.13 (Sun Jan 12 2025)

🐛 Bug Fix

📝 Documentation

  • update readme, add PR checks #5 (@igrlk)

Authors: 1


v0.0.12 (Sat Jan 11 2025)

🐛 Bug Fix

Authors: 1


v0.0.11 (Wed Jan 08 2025)

🐛 Bug Fix

  • don't rely on workspace dependency from pnpm, use ^8 instead #3 (@igrlk)

Authors: 1


v0.0.10 (Tue Jan 07 2025)

⚠️ Pushed to main

  • add test keyword (iluchenkov@gmail.com)

Authors: 1

  • igrlk (iluchenkov@gmail.com)

v0.0.9 (Tue Jan 07 2025)

⚠️ Pushed to main

  • add keywords for storybook catalog (iluchenkov@gmail.com)

Authors: 1

  • igrlk (iluchenkov@gmail.com)

v0.0.8 (Thu Jan 02 2025)

🐛 Bug Fix

⚠️ Pushed to main

  • changelog (@igrlk)
  • Merge branch 'main' of github.com:igrlk/storybook-addon-test-codegen (@igrlk)
  • Initial commit (@igrlk)

Authors: 1


v0.0.7 (Wed Jan 01 2025)

⚠️ Pushed to main

Authors: 1