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 🙏

© 2024 – Pkg Stats / Ryan Hefner

storybook-addon-vis

v0.4.0

Published

Storybook Vitest Image Snapshot addon

Downloads

1,749

Readme

storybook-addon-vis

Storybook Vitest Image Snapshot add-on.

Captures and compare image snapshot automatically and manually.

This add-on is inspired by jest-image-snapshot.

Starting from storybook 8.3, you can run Storybook stories with vitest browser mode.

Since it is running in actual browser, jest-image-snapshot does not work. This add-on provides similar functionality to jest-image-snapshot. In addition, you can capture image snapshot manually.

Install

npm install --save-dev storybook-addon-vis

pnpm add --save-dev storybook-addon-vis

yarn add --save-dev storybook-addon-vis

Config

This add-on provides features on both storybook and vitest, thus you need to add it to both storybook and vitest.

On vitest, you need to add storybook-addon-vis/vitest-plugin plugin in vitest.config.ts.

// vite.config.ts or vitest.config.ts
import { storybookTest } from '@storybook/experimental-addon-test/vitest-plugin'
import { storybookVis } from 'storybook-addon-vis/vitest-plugin'
import { defineConfig } from 'vitest/config'

export default defineConfig({
   plugins: [
    storybookTest(),
    storybookVis()
	],
  test: {
    browser: {
      // ...
    }
    setupFiles: ['./vitest.setup.ts'],
  }
})

Also note that you need to add vitest.setup.ts file to set up the test environment. You need to do that anyway when you set up storybook with vitest.

In vitest.setup.ts, you need to extend vitest expect and register lifecycle hooks so that storybook-addon-vis can access the test context info to capture image snapshot.

// vitest.setup.ts
import { type StoryContext, setProjectAnnotations } from '@storybook/react'
import {
  configureSnapshotBeforeAll,
  configureSnapshotBeforeEach,
  page,
  shouldTakeSnapshot,
  toMatchImageSnapshot,
} from 'storybook-addon-vis/vitest-setup'
import { afterEach, beforeAll, beforeEach, expect } from 'vitest'
import * as projectAnnotations from './preview'

const project = setProjectAnnotations([projectAnnotations])

expect.extend({ toMatchImageSnapshot })

beforeAll(async (suite) => {
  project.beforeAll()
  await configureSnapshotBeforeAll(suite, /* options */)
})

beforeEach(configureSnapshotBeforeEach)

afterEach<{ story?: StoryContext }>(async (ctx) => {
  if (!shouldTakeSnapshot(ctx)) return

  await expect(page.imageSnapshot()).toMatchImageSnapshot(ctx.story?.parameters.snapshot)
})

On storybook, you need to extend the expect from @storybook/test

// .storybook/preview.tsx
import { expect } from '@storybook/test'
import { toMatchImageSnapshot } from 'storybook-addon-vis'

expect.extend({ toMatchImageSnapshot })

// ...

Usage - automatic snapshot

The afterEach hook above is how to capture the image snapshot automatically, when you add a snapshot tag to the story.

// story.tsx
export default {
  title: 'Button',
  // Take image snapshot automatically for all stories in this file
  tags: ['snapshot']
}

export const MyStory = {
  // Take image snapshot automatically for this story
  tags: ['snapshot'],
  // ...
}

You can provide options to the automatic toMatchImageSnapshot matcher using parameters.

import { defineSnapshotParam } from 'storybook-addon-vis'

export const MyStory = {
  // Take image snapshot automatically for this story
  tags: ['snapshot'],
  parameters: defineSnapshotParam({
    failureThreshold: 70,
  })
  // ...
}

You can disable snapshot capturing with the !snapshot tag, much like !test.

export default {
  title: 'Button',
  tags: ['snapshot']
}

export const MyStory = {
  // Disable image snapshot for this story
  tags: ['!snapshot'],
  // ...
}

Usage - manual snapshot

Besides automatic snapshot, you can capture image snapshot manually.

import { expect } from '@storybook/test'

// `page` and the like are proxy to `@vitest/browser/context` to work in storybook
import { page } from 'storybook-addon-vis'

export const PageSnapshot = {
  // ...
  async play() {
    await expect(page.imageSnapshot()).toMatchImageSnapshot()
  }
}

export const ElementSnapshot = {
  // ...
  async play({ canvas }) {
    const element = await canvas.getByTestid('subject')
    await expect(page.imageSnapshot({ element })).toMatchImageSnapshot()
  }
}