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

@shutter/react

v0.5.0

Published

React.js visual snapshot testing library using [shutter.sh](https://shutter.sh).

Downloads

11

Readme

@shutter/react NPM Version

React.js visual snapshot testing library using shutter.sh.

Renders React components to PNG snapshots using the shutter.sh service and saves snapshots locally. Future test runs will re-render the content and diff it against your previously saved snapshot.

The tests will fail if the current snapshot does not match the expected one. If that visual change was intended, you can update your local snapshot.

Installation

npm install --save-dev @shutter/core
# or using yarn:
yarn add --dev @shutter/core

Usage

import createReactShutter from '@shutter/react'
import Button from './button'

const shutter = createReactShutter(__dirname)

describe('Button component', function () {
  after(async function () {
    // Collect and evaluate results once we are done
    await shutter.finish()
  })

  it('matches visual snapshot', async function () {
    await Promise.all([
      shutter.snapshot('Default Button', <Button label='Click me' />),
      shutter.snapshot('Primary Button', <Button primary label='Click me' />)
    ])
  })
})

When running this test, @shutter/react will render your button in the desired states and compare the resulting snapshots to your previous component snapshots.

It will also print an inspection URL which links to the shutter.sh app where you can see the rendered component, the expected outcome and a visual diff between them.

API

import createReactShutter from '@shutter/react'

const shutter = createReactShutter(__dirname)

async function run () {
  await shutter.snapshot('List', <ul><li>List item</li></ul>)
  await shutter.finish()
}

run()

createReactShutter(testDirectoryPath: string, options: ShutterOptions): Shutter

Creates a shutter instance. You need to pass your testing directory (can usually just use __dirname), so it knows where to save the snapshots.

interface ShutterOptions {
  /** Local files to upload, like stylesheets. Use `addFile()` to populate this array. */
  files?: File[],

  /** Custom content to go into the <head> tag of the document. */
  head?: ReactElement<any>,

  /** Layout to use for rendering. Pass a custom layout to change the overall page structure. */
  layout?: (bodyContent: string, headContent: string) => string,

  /** Render function to use. Takes some React element tree and renders it to static HTML. */
  render?: (reactElement: ReactElement<any>) => Promise<string>,

  /** Set a custom path to your local snapshot files here. */
  snapshotsPath?: string,

  /** Set custom image comparison options here. Used to compare the current snapshot to the expectation. */
  diffOptions?: DiffOptions,

  /** Set custom rendering options here. */
  renderOptions?: RenderOptions
}

Check out the @shutter/api documentation for the DiffOptions and RenderOptions details.

shutter.snapshot(testName: string, element: ReactElement<any>, options: SnapshotOptions = {}): Promise<void>

Send page contents to the shutter.sh service to be rendered.

The returned promise will resolve once the upload is done, but before the rendering has finished. That is why you need to call shutter.finish() after calling shutter.snapshot() the last time.

interface SnapshotOptions {
  layout?: (htmlContent: string) => string,
  render?: (reactElement: ReactElement<any>) => Promise<string>,
  diffOptions?: DiffOptions,
  renderOptions?: RenderOptions
}

The options are mostly the same as the ShutterOptions. They can be used to override the options per test case.

Check out the @shutter/api documentation for the DiffOptions and RenderOptions details.

shutter.finish(): Promise<TestResult[]>

Waits until all rendering tasks have finished, then collects and evaluates the results.

Will throw with a test results summary if snapshots don't match. Prints a success message and an inspection link if everything matched.

addFile(localPath: string, serveAsPath: string): Promise<File>

Reads a local file and prepares it for submission along the HTML content to render. Use it to submit local stylesheets, images, etc.

Pass the resulting File to createShutter() as options.files.

Note: The submitted file will be publicly accessible.

createFileFromBuffer(content: Buffer, fileName: string, options: FileCreationOptions = {}): File

Allows you to submit a file from in-memory contents. Check out the @shutter/api documentation for details.

Custom render function

You can pass a custom render function to createReactShutter() or shutter.snapshot(). The render function takes a React element and returns a promise resolving to a string of static HTML. The default render function, exported as renderComponent, is just a thin wrapper around ReactDOMServer.renderToStaticMarkup().

export type HTMLString = string
export type BuiltinRenderFunction = (reactElement: ReactElement<any>) => Promise<HTMLString>
export type RenderFunction = (reactElement: ReactElement<any>) => Promise<HTMLString>

Use it wrap your components in other components, like context providers:

import { Provider } from 'react-redux'
import createReactShutter, { renderComponent as baseRender } from '@shutter/react'
import store from './store'

const render = (element) => {
  return baseRender(
    <Provider store={store}>
      {element}
    </Provider>
  )
}

const shutter = createReactShutter(__dirname, { render })

See also

Check out the documentation at https://docs.shutter.sh.