# jest-canvas-snapshot-serializer

> jest snapshot serializer for canvas elements

Latest version **1.0.1** (published 2018-01-25) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install jest-canvas-snapshot-serializer
pnpm add jest-canvas-snapshot-serializer
yarn add jest-canvas-snapshot-serializer
bun add jest-canvas-snapshot-serializer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-01-25 |
| First published | 2018-01-21 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Benjamin Seber |
| Maintainers | bseber |
| Keywords | jest, test |

## Links

- npm: https://www.npmjs.com/package/jest-canvas-snapshot-serializer
- Repository: https://github.com/synyx/jest-canvas-snapshot-serializer
- Homepage: https://github.com/synyx/jest-canvas-snapshot-serializer#readme
- Issues: https://github.com/synyx/jest-canvas-snapshot-serializer/issues
- npm.io page: https://npm.io/package/jest-canvas-snapshot-serializer

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2018-01-25
- 1.0.0 — 2018-01-21

## README

# jest-canvas-snapshot-serializer

[![build status][travis-image]][travis-url]
[![coverage status][codecov-image]][codecov-url]

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

```js
// 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="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAADICAYAAABS39xVAAAABm...."
/>
`
```

The snapshot now contains the base64 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


[travis-image]: https://img.shields.io/travis/synyx/jest-canvas-snapshot-serializer.svg?style=flat-square
[travis-url]: https://travis-ci.org/synyx/jest-canvas-snapshot-serializer
[codecov-image]: https://img.shields.io/codecov/c/github/synyx/jest-canvas-snapshot-serializer.svg?style=flat-square
[codecov-url]: https://codecov.io/gh/synyx/jest-canvas-snapshot-serializer

---
_Source: https://npm.io/package/jest-canvas-snapshot-serializer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
