GithubHelp home page GithubHelp logo

szimek / jest-canvas-snapshot-serializer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from synyx/jest-canvas-snapshot-serializer

0.0 2.0 0.0 104 KB

Jest snapshot serializer to generate comparable canvas snapshots

License: Apache License 2.0

JavaScript 100.00%

jest-canvas-snapshot-serializer's Introduction

jest-canvas-snapshot-serializer

build status coverage status

Jest Snapshot Serializer to create comparable snapshots of canvas elements.

Usage

Make sure you're either running jest with environment set to jsdom (default setting) or you have setup jsdom in the test setup file.

Install canvas-prebuilt and this module

npm install --save-dev canvas-prebuilt jest-canvas-snapshot-serializer

and add it as serializer

// myTest.spec.js
import canvasSerializer from "jest-canvas-snapshot-serializer";

expect.addSnapshotSerializer(canvasSerializer);

test("my awesome test", () => {
    const canvas = document.createElement("canvas");

    // canvas must have a width and height attribute
    // otherwise there is no image to serialize
    canvas.setAttribute("width", "200");
    canvas.setAttribute("height", "200");

    drawAwesomeImage(canvas);

    expect(canvas).toMatchSnapshot();
});

Running the test creates a snapshot file like:

exports["my awesome test 1"] = `
<canvas
  width="200"
  height="200"
  data-snapshot-image="51e09c5637c8c4cf463ce0da78329bcca119..."
/>
`

The snapshot now contains a hashed representation of the drawn image. So we're informed about canvas image regressions \o/

Furthermore this serializer will create an image file next to the snapshot file.

.
├── __snapshots__
│   ├── myTest.spec.js.snap
│   ├── myTest.spec.js.snap.my-awesome-test.canvas-image.png
│   └── myTest.spec.js.snap.my-awesome-test.canvas-image.dirty.png
└── myTest.spec.js

There is also a dirty image file if jest is running without --updateSnapshot and the persisted snapshot doesn't match the current implementation. So you can compare the original/persisted image with the current one. The dirty image is deleted as soon as jest updates the snapshot.

FAQ

Why a snapshot serializer instead of a custom matcher like .toMatchCanvasSnapshot?

Using a custom matcher we'd have to implement the same stuff already provided by jest-snapshot (e.g. success and error feedback). Whereas using a serializer we only have to take care about the serialization of the canvas element and writing/deleting the image files.

License

Apache License 2.0

jest-canvas-snapshot-serializer's People

Contributors

bseber avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.