Giter Site home page Giter Site logo

jest-canvas-snapshot-serializer's Introduction

jest-canvas-snapshot-serializer

build status coverage status

Jest Snapshot Serializer to create comparable snapshots of canvas elements.

๐Ÿ˜ฟ this project is deprecated.

an option may be to use playwright and jest-image-snapshot

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 and this module

npm install --save-dev canvas 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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jest-canvas-snapshot-serializer's Issues

TypeError: prettyFormat is not a function

I just get the following error trying to use this:

TypeError: prettyFormat is not a function
at format (node_modules/jest-canvas-snapshot-serializer/src/canvasSnapshotSerializer.js:3:12)
      at Object.print (node_modules/jest-canvas-snapshot-serializer/src/canvasSnapshotSerializer.js:62:25)
      at printPlugin (node_modules/pretty-format/build/index.js:330:16)
      at format (node_modules/pretty-format/build/index.js:561:16)
      at __EXTERNAL_MATCHER_TRAP__ (node_modules/expect/build/index.js:386:30)
      at Object.toMatchSnapshot (node_modules/expect/build/index.js:387:15)
      at Object.<anonymous> (src/components/Plots/Scatter/Scatter/Scatter.unit.js:172:28)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:401:19)

I've tried manually installing pretty-format as a dev dependency afterwards but that didn't fix anything unfortunately.

Cannot read property 'test' of undefined

I've created a chart.js plugin that I now want to test. I'm running this as documented I get the following error:

TypeError: Cannot read property 'test' of undefined

Here is my test:

  let canvas: any;
  beforeEach( () => {
    canvas = document.createElement('canvas');
    canvas.setAttribute('width', '100');
    canvas.setAttribute('height', '100');
  });
  describe( 'plugin', () => {
    it('renders canvas', () => {
      plugins.plugin.beforeDatasetDraw( {
        config: {
          type: 'doughnut'
        },
        outerRadius: 100,
        innerRadius: 80,
        chartArea: {
         right: 100,
         bottom: 100
        },
        ctx: canvas.getContext('2d')
      });
      expect( canvas ).toMatchSnapshot();
    });
  });

The plugin I'm testing:

const plugin = {
  id: 'plugin',
  beforeDatasetDraw: (chart: any) => {
    if (chart.config.type === 'doughnut') {
      const ctx = chart.ctx;
      ctx.beginPath();
      ctx.strokeStyle = '#D2D2D2';
      ctx.arc(
        chart.chartArea.right / 2,
        chart.chartArea.bottom / 2,
        (chart.outerRadius + chart.innerRadius) / 2,
        0,
        2 * Math.PI
      );
      ctx.stroke();
    }
  }
};

Hashing doesn't work

According to the documentation the snapshot should contain a hash, but when I try the example this is my result:

<canvas
  data-snapshot-image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAA6e...[FULL_BASE_64]"
  height="200"
  width="200"
/>

My test code:

import canvasSerializer from 'jest-canvas-snapshot-serializer';
expect.addSnapshotSerializer(canvasSerializer);

it('works with canvas', () => {
    const canvas = createCanvas('#000', '#F00'); // function from your example.
    expect(canvas).toMatchSnapshot();
  });

Am I missing something?

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.