Storybook interaction tests let you test user interactions inside your stories. They enhance your Storybook setup, so that your components not only look right but also behave correctly when interacted with.
Setting up Storybook interaction tests
Section titled “Setting up Storybook interaction tests”Prerequisites
Section titled “Prerequisites”You need Storybook already set up in your Nx workspace. If you don't have Storybook set up yet, read the Storybook plugin overview guide.
Generate Storybook configuration with interaction tests
Section titled “Generate Storybook configuration with interaction tests”When generating your Storybook configuration, answer Yes to the Do you want to set up Storybook interaction tests? prompt, or pass the --interactionTests=true flag.
nx g @nx/storybook:configuration my-project --interactionTests=trueThis installs @storybook/test-runner at a version that matches your Storybook major, and the @nx/storybook/plugin then infers a test-storybook target for the project.
Writing interaction tests
Section titled “Writing interaction tests”Write interaction tests in your stories using the play function:
import type { Meta, StoryObj } from '@storybook/react-vite';import { expect, userEvent, within } from 'storybook/test';
import { Button } from './button';
const meta: Meta<typeof Button> = { title: 'Example/Button', component: Button, parameters: { layout: 'centered', }, argTypes: { backgroundColor: { control: 'color' }, },};
export default meta;type Story = StoryObj<typeof meta>;
export const Primary: Story = { args: { primary: true, label: 'Button', }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); const button = canvas.getByRole('button');
await expect(button).toBeInTheDocument(); await userEvent.click(button); await expect(button).toHaveFocus(); },};Running interaction tests
Section titled “Running interaction tests”The test runner drives a Storybook that is already serving. Start Storybook in one terminal and leave it running:
nx storybook my-projectRun the interaction tests from a second terminal:
nx test-storybook my-projectYou can also point the test runner at a published Storybook with --url.
Using the Vitest addon instead
Section titled “Using the Vitest addon instead”Storybook recommends @storybook/addon-vitest for Vite-powered frameworks. Nx doesn't configure it for you. Use Storybook 10 or later. On Storybook 9 the installer writes its Vitest config next to the directory you run it from, not next to the project. The inferred target runs from the project root, so it doesn't find the storybook Vitest project.
From the workspace root, replace <project-root> with the project's root directory and run:
npx storybook add @storybook/addon-vitest --config-dir <project-root>/.storybookThis command registers the addon, creates the storybook Vitest project, and installs the required browser dependencies. Once the addon is installed, @nx/storybook/plugin infers test-storybook as a Vitest run against that project rather than a test-runner command, for projects whose Storybook framework uses the Vite builder.
Debugging interaction tests
Section titled “Debugging interaction tests”During development, watch your interaction tests run in the Storybook UI:
- Start your Storybook:
nx storybook my-project - Open the Interactions panel in the Storybook UI
- Run individual story tests by clicking the play button
Step through the tests from that panel to debug any failures.