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

@spring-media/storybook-addon-image-snapshots

v0.11.0

Published

Storybook plugin for taking image snapshots of your stories based on @storybook/addon-storyshots-puppeteer plugin.

Downloads

133

Readme

Intro

This plugin uses @storybook/storyshots-puppeteer to capture screenshots from your stories. It provided some additional features like:

  • capturing specific viewports (dependency on @storybook/addon-viewport)
  • capture only specific elements on the page
  • adds a panel in the storybook ui to display the snapshots However, the main purpose is to run the tests against a (chrome) browser that is running in a docker container, please see Why docker? for a detailed explanation.

Table of contents

Getting Started

Installation

Install the following package:

npm i @spring-media/storybook-addon-image-snapshots -D

Usage

Within the .storybook/main.js add the package to the plugins list:

module.exports = {
  addons: ['@spring-media/storybook-addon-image-snapshots']
}

This is optional if you don't want to display the snapshots within storybook

Create a file image-snapshots.runner.js with the following content:

The file should not be suffixed with '.test.js' to avoid being run with all other test files

import { initImageSnapshots } from '@spring-media/storybook-addon-image-snapshots';

initImageSnapshots();

Add the following entry to the script section of your package.json:

"scripts": {
  "image-snapshots": "jest --config your-specific-jest.config.js image-snapshots.runner.js"
}

Do not forget to add a rule for the 'runner.js' suffix in jest's testMatch option.

Testing with a local running storybook server

By default, the plugin expects that a local storybook server is running on the port 9001.

Start the docker container with the following arguments:

docker run -p 9222:3000 --rm -d --name chrome browserless/chrome

Then run the previously added script:

npm run image-snapshots

Testing with a local static storybook build

There are a few things that has to be changed in order to test a static build of storybook:

docker run -p 9222:3000 -d --rm --name chrome -v $(pwd)/storybook-static/:/opt/storybook-static browserless/chrome

By default, storybook will create a storybook-static directory that contains all static files

Since the tests are running in a docker container, we must mount the local static directory into the containers file system

Change the storybookUrl option (see Custom Configuration) to file:///opt/storybook-static.

Testing different viewports

Testing different viewports requires the official @storybook/addon-viewport to be installed and activated.

Below is an example how to configure your story with the viewport configuration:

export const myStory = () => ({...});
myStory.story = {
  parameters: {
    viewport: {
      defaultViewport: 'small'
    }
  }
};

This example assumes, that a viewport plugin has a configuration entry with a key "small".

Select a specific element for taking a snapshot

You can configure the story to take a snapshot only of a certain element by providing a selector.

export const myStory = () => ({...});
myStory.story = {
  parameters: {
    imageSnapshot: {
      selector: '.my-element'
    }
  }
};

Disable image snapshots for specific stories

To disable specific stories from generating image snapshots, configure your story as follows:

export const myStory = () => ({...});
myStory.story = {
  parameters: {
    storyshots: {
      disable: true,
    }
  }
};

Custom Configuration

Since this plugin is only a wrapper around storyshots-puppeteer, the signature of initImageSnapshots is the same as initStoryshots. But, you should not customize getCustomBrowser, beforeScreenshot and getScreenshotOptions since there are used internally by this plugin.

browserUrl

This url should point to the running docker container (default: http://localhost:9222)

storybookUrl

This is the url to your storybook server that is called from within the docker container (default: http://host.docker.internal:9001)

Make sure your storybook server run on port 9001

You may ask what the heck is this host and where does it come from: host.docker.internal? Find more information here: docker networking why we need to set this host.

Display snapshots in storybook

This plugin adds a panel in the storybook ui to display the image snapshot of a story.

First register the addon in the main.js file:

module.exports = {
  addons: ['@spring-media/storybook-addon-image-snapshots']
}

Then you have to tell the panel where to find the image. You can do this by either add a global parameter in the preview.js file:

import { addParameters } from '@storybook/vue';

addParameters({
  imageSnapshots: {
    snapshotFileName: ({ id }) => `storyshots-${id}-snap.png`,
  },
});

or in the configuration of a story:

export const myStory = () => ({...});
myStory.story = {
  parameters: {
    imageSnapshots: {
      snapshotFileName: ({ id }) => `storyshots-${id}-snap.png`,
    }
  }
};

You can customize the filename of the snapshot through getMatchOptions

The last thing is to add the directory of your image snapshots to the static-dir configuration of the storybook server:

{
    "scripts": {
        "storybook": "start-storybook -p 8888 -s .image-snapshots"
    }
}

The example above assumes, that the images are be located within the .image-snapshots directory.

Notes

Why Docker?

There are some challenges when it comes to capture screens from a browser as described here. Because of that, we need a way to create a result that is as much as possible independent of the host system. By using docker, we get an isolated system that is reliable enough to produce the same results on different host systems.