Storybook Vitest Addon

0.1.8 · active · verified Tue Apr 21

The `storybook_vitest_addon` integrates Vitest unit test results directly into a Storybook panel, enhancing the development workflow by displaying relevant test outcomes alongside component stories. This addon is currently at version `0.1.8` and has a relatively frequent release cadence, often aligning with Storybook and Vitest major version updates, as seen with recent upgrades to Storybook 8. It primarily supports React and Vite-based Storybook setups, requiring `vitest` v1.1.0 or newer. Its key differentiator is its minimal visual overhead, focusing on clarity for test results, and its direct integration via Storybook parameters, making it straightforward to link test output (JSON format) to individual stories. Developers provide a JSON file of Vitest results and the test file name to the addon's parameters to enable the display.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the `storybook_vitest_addon` in a Storybook story file, linking Vitest test results to a component.

import { Meta, StoryObj } from '@storybook/react';
import vitestResults from './unit-test-results.json'; // Assume this file is generated by Vitest

interface ButtonProps {
  label: string;
  primary?: boolean;
}

// Dummy Button Component for example
const Button = ({ primary = false, label }: ButtonProps) => {
  const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
  return (
    <button type="button" className={['storybook-button', mode].join(' ')}>
      {label}
    </button>
  );
};

const meta: Meta<typeof Button> = {
  title: 'Example/Button',
  component: Button,
  parameters: {
    vitest: {
      testFile: 'Button.test.tsx', // Name of the Vitest test file for this component
      testResults: vitestResults, // The imported JSON object of test results
    },
  },
  argTypes: {
    backgroundColor: { control: 'color' },
  },
};

export default meta;
type Story = StoryObj<typeof Button>;

export const Primary: Story = {
  args: {
    primary: true,
    label: 'Button',
  },
};

export const Secondary: Story = {
  args: {
    label: 'Button',
  },
};

view raw JSON →