@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
Install
design-matcher
andjest
dependencies.npm install @engi.network/design-matcher jest ts-jest @types/jest
Add
test
script to yourpackage.json
."scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint", "test": "jest" },
Add the type declarations file to
files
intsconfig.json
.{ "compilerOptions": { // ... }, "exclude": ["node_modules"], "files": [ "node_modules/@engi.network/design-matcher/src/index.d.ts" ] }
Create a
jest.setup.ts
file for extending the custom matchers to use.toMatchDesign()
. Also usedotenv
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 });
Update your
jest.config.js
to run the setup file.module.exports = { roots: ["<rootDir>"], preset: "ts-jest", testEnvironment: "node", setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"], };
Install Storybook.
Modify your
./storybook/preview.ts
file to remove padding around the component. This is needed aspuppeteer
will launch an instance ofchromium
and take a screenshot with the exact dimensions of the design image.const preview: Preview = { parameters: { ... // remove default padding around components layout: "fullscreen", }, };
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
Start Storybook in your repository.
npm run storybook
Run
npm test
.npm test