jest-image-snapshot
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -15,2 +15,3 @@ /* | ||
/* eslint-disable global-require */ | ||
const fs = require('fs'); | ||
@@ -17,0 +18,0 @@ const path = require('path'); |
@@ -15,2 +15,3 @@ /* | ||
/* eslint-disable global-require */ | ||
const fs = require('fs'); | ||
@@ -17,0 +18,0 @@ const path = require('path'); |
@@ -32,3 +32,3 @@ /* | ||
intercept.toMatchImageSnapshot = function (...args) { | ||
intercept.toMatchImageSnapshot = function toMatchImageSnapshot(...args) { | ||
const ctx = this; | ||
@@ -58,5 +58,7 @@ let originalUpdateSnapshot = null; | ||
const cleanSnapshot = function (customSnapshotIdentifier) { | ||
const snapPath = path.join(snapDir, `${customSnapshotIdentifier}-snap.png`); | ||
const getSnapshotBasename = identifier => `${identifier}-snap.png`; | ||
const cleanSnapshot = (customSnapshotIdentifier, snapshotsDir = snapDir) => { | ||
const snapPath = path.join(snapshotsDir, getSnapshotBasename(customSnapshotIdentifier)); | ||
if (fs.exists(snapPath)) { | ||
@@ -117,2 +119,15 @@ fs.unlink(snapPath); | ||
}); | ||
it('creates snapshot in custom directory if such is specified.', () => { | ||
const customSnapshotsDir = path.resolve(__dirname, '__custom_snapshots_dir__'); | ||
const customSnapshotIdentifier = 'integration-5'; | ||
cleanSnapshot(customSnapshotIdentifier, customSnapshotsDir); | ||
// First we need to write a new snapshot image | ||
expect(() => expect(imageData).toMatchImageSnapshot({ customSnapshotsDir, customSnapshotIdentifier })).not.toThrowError(); // eslint-disable-line max-len | ||
// Then we check if the file was created in custom directory | ||
expect(() => fs.readFileSync(path.resolve(customSnapshotsDir, getSnapshotBasename(customSnapshotIdentifier)))).not.toThrowError(); // eslint-disable-line max-len | ||
}); | ||
}); |
{ | ||
"name": "jest-image-snapshot", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Jest matcher for screenshot comparisons. Most commonly used for browser visual testing.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -39,4 +39,5 @@ # jest-image-snapshot | ||
* `customDiffConfig`: Custom config passed [pixelmatch](https://github.com/mapbox/pixelmatch#pixelmatchimg1-img2-output-width-height-options) (See options section) | ||
* By default we have set the `threshold` to 0.01, you can increase that value by passing a customDiffConfig as demonstrated below. | ||
* By default we have set the `threshold` to 0.01, you can increase that value by passing a customDiffConfig as demonstrated below. | ||
* Please note the `threshold` set in the `customDiffConfig` is the per pixel sensitivity threshold. For example with a source pixel colour of `#ffffff` (white) and a comparison pixel colour of `#fcfcfc` (really light grey) if you set the threshold to 0 then it would trigger a failure *on that pixel*. However if you were to use say 0.5 then it wouldn't, the colour difference would need to be much more extreme to trigger a failure on that pixel, say `#000000` (black) | ||
* `customSnapshotsDir`: (default: `__image_snapshots__`) A custom directory to keep this snapshot in | ||
* `customSnapshotIdentifier`: A custom name to give this snapshot. If not provided one is computed automatically | ||
@@ -85,3 +86,3 @@ * `noColors`: (default `false`) Removes colouring from console output, useful if storing the results in a file | ||
## How it works | ||
Given an image (should be either a PNGImage instance or a Buffer instance with PNG data) the `toMatchImageSnapshot()` matcher will create a `__image_snapshots__` directory in the directory the test is in and will store the baseline snapshot image there on the first run. | ||
Given an image (should be either a PNGImage instance or a Buffer instance with PNG data) the `toMatchImageSnapshot()` matcher will create a `__image_snapshots__` directory in the directory the test is in and will store the baseline snapshot image there on the first run. Note that if `customSnapshotsDir` option is given then it will store baseline snapshot there instead. | ||
@@ -88,0 +89,0 @@ On subsequent test runs the matcher will compare the image being passed against the stored snapshot. |
@@ -21,2 +21,4 @@ /* | ||
const SNAPSHOTS_DIR = '__image_snapshots__'; | ||
function updateSnapshotState(oldSnapshotState, newSnapshotState) { | ||
@@ -34,2 +36,3 @@ return merge({}, oldSnapshotState, newSnapshotState); | ||
customSnapshotIdentifier = '', | ||
customSnapshotsDir, | ||
customDiffConfig = {}, | ||
@@ -52,3 +55,3 @@ noColors = commonNoColors, | ||
snapshotIdentifier, | ||
snapshotsDir: path.join(path.dirname(testPath), '__image_snapshots__'), | ||
snapshotsDir: customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR), | ||
updateSnapshot: snapshotState._updateSnapshot === 'all', | ||
@@ -91,2 +94,3 @@ customDiffConfig: Object.assign({}, commonCustomDiffConfig, customDiffConfig), | ||
updateSnapshotState, | ||
SNAPSHOTS_DIR, | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
883804
29
868
135