Skip to content

Commit

Permalink
feat(dependencies): Move jest as peerDependencies and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
donysukardi committed May 7, 2018
1 parent f548455 commit 821744d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
# jest-img-snapshot

Inspired by jest-image-snapshot
Jest matcher for image comparisons using [pixelmatch](https://github.com/mapbox/pixelmatch) with all the goodness of Jest snapshots out of the box.

This project was heavily inspired by [jest-image-snapshot](https://github.com/americanexpress/jest-image-snapshot).

## Overview

Internally, it uses Jest's snapshot features by keeping track of snapshot changes using checksum of the serialized image, passing and failing the snapshot tests accordingly. This means you get interactive update for failing snapshot tests, removal of obsolete snapshots out of the box.

Note that when update flag is not specified and the image comparison falls within the specified threshold, the library will not perform any update and pass the test using the previously stored checksum.

## Usage

1. Extend Jest's `expect` with `toMatchImageSnapshot` exposed by the library

```js
const { toMatchImageSnapshot } = require('jest-image-snapshot');
expect.extend({ toMatchImageSnapshot });
```

2. Or use `configureToMatchImageSnapshot` to specify default configuration (refer to Optional Configuration below for details)

```js
const { configureToMatchImageSnapshot } = require('jest-image-snapshot');
const toMatchImageSnapshot = configureToMatchImageSnapshot({
customDiffConfig: { threshold: 0.1 },
noColors: false,
failureThresholdType: 'percent',
failureThreshold: 0.01
});
expect.extend({ toMatchImageSnapshot });
```

2. Finally, use `expect(...).toMatchImageSnapshot()` in your tests

```js
it('should match image snapshot', () => {
// ...
expect(img).toMatchImageSnapshot();
});
```

### Optional configuration

The following configuration can be passed as object to both `configureToMatchImageSnapshot` as default and `expect(...).toMatchImageSnapshot` as override for particular tests,
* `customDiffConfig`, options passed to [pixelmatch](https://github.com/mapbox/pixelmatch#api). Default: `{ threshold: 0.01 }`
* `noColors`, flag to disable chalk coloring. Default: `false`
* `failureThresholdType`, used in conjuction with `failureThreshold`, options are `pixel` (default) or `percent`
* `failureThreshold`, used in conjunction with `failureThresholdType`, for `percent`, it ranges from 0-1. Default: 0

The following configuration can be used in conjunction with configuration above for `expect(...).toMatchImageSnapshot()` only,
* `name`, custom name for the snapshot, as passed to https://facebook.github.io/jest/docs/en/expect.html#tomatchsnapshotoptionalstring
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"author": "Dony Sukardi <ds@dsds.io> (https://github.com/donysukardi)",
"dependencies": {
"get-stdin": "^6.0.0",
"jest": "^22.4.3",
"jest-matcher-utils": "^22.4.3",
"jest-snapshot": "^22.4.3",
"lodash.kebabcase": "^4.1.1",
"mkdirp": "^0.5.1",
"pixelmatch": "^4.0.2",
Expand All @@ -33,7 +30,13 @@
"eslint-plugin-react": "^7.1.0",
"prettier": "^1.12.1",
"semantic-release": "^15.2.0",
"travis-deploy-once": "^5.0.0"
"travis-deploy-once": "^5.0.0",
"jest": "^22.4.3",
"jest-matcher-utils": "^22.4.3",
"jest-snapshot": "^22.4.3"
},
"peerDependencies": {
"jest": ">=20 <=22"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 821744d

Please sign in to comment.