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

@engi.network/design-matcher

v1.1.18

Published

This is a jest plugin for comparing storybook stories with design images.

Downloads

1

Readme

Design Matcher

This is a jest plugin for comparing storybook stories with design images.

Setup

  1. Install design-matcher and jest dependencies.

    npm install @engi.network/design-matcher jest ts-jest @types/jest
  2. Add test script to your package.json.

    "scripts": {
       "dev": "next dev",
       "build": "next build",
       "start": "next start",
       "lint": "next lint",
       "test": "jest"
    },
  3. Add the type declarations file to files in tsconfig.json.

    {
      "compilerOptions": {
        // ...
      },
      "exclude": ["node_modules"],
      "files": [
        "node_modules/@engi.network/design-matcher/src/index.d.ts"
      ]
    }
  4. Create a jest.setup.ts file for extending the custom matchers to use .toMatchDesign(). Also use dotenv to load environment variables.

    import { toMatchDesign } from "@engi.network/design-matcher";
    import dotenv from "dotenv";
    
    dotenv.config();
    // optionally specify a file other than .env
    // dotenv.config({ path: "./.env.development.local" });
    
    expect.extend({ toMatchDesign });
  5. Update your jest.config.js to run the setup file.

    module.exports = {
      roots: ["<rootDir>"],
      preset: "ts-jest",
      testEnvironment: "node",
      setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
    };
  6. Install Storybook.

  7. Modify your ./storybook/preview.ts file to remove padding around the component. This is needed as puppeteer will launch an instance of chromium and take a screenshot with the exact dimensions of the design image.

    const preview: Preview = {
      parameters: {
        ...
        // remove default padding around components
        layout: "fullscreen",
      },
    };
  8. Set STORYBOOK_PORT environment variable for port that storybook is running on.

    STORYBOOK_PORT=6006
    
    // optionally specify the diff threshold and the temp dir
    DESIGN_DIFF_THRESHOLD=0.05
    DESIGN_TEMP_DIR=temp

Writing tests

Write a test using the toMatchDesign() custom matcher.

import { StorybookTest } from "@engi.network/design-matcher/plugin/types";

const mockTest: StorybookTest = {
  component: "Button",
  story: "Primary",
  args: {
    variant: "Primary",
  },
  design: "plugin/designs/primary-button.png",
};

test("should match design", async () => {
  await expect(mockTest).toMatchDesign();
});

// Optionally, specify a background color for the design image if it is transparent
test("should match design", async () => {
  await expect(mockTest).toMatchDesign({ backgroud: "#000000" });
});

Running

  1. Start Storybook in your repository.

    npm run storybook
  2. Run npm test.

    npm test