Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-image-snapshot

Package Overview
Dependencies
Maintainers
4
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-image-snapshot - npm Package Compare versions

Comparing version 2.4.2 to 2.4.3

examples/image-reporter.js

16

__tests__/index.spec.js

@@ -38,2 +38,4 @@ /*

beforeEach(() => {
// In tests, skip reporting (skip snapshotState update to not mess with our test report)
global.UNSTABLE_SKIP_REPORTING = true;
jest.resetModules();

@@ -344,2 +346,3 @@ jest.resetAllMocks();

customDiffConfig: customConfig,
customSnapshotsDir: 'path/to/my-custom-snapshots-dir',
noColors: true,

@@ -357,3 +360,3 @@ });

snapshotIdentifier: 'test-spec-js-test-1-1',
snapshotsDir: path.join('path', 'to', '__image_snapshots__'),
snapshotsDir: path.join('path', 'to', 'my-custom-snapshots-dir'),
updateSnapshot: false,

@@ -368,1 +371,12 @@ failureThreshold: 0,

});
describe('updateSnapshotState', () => {
it('mutates original state', () => {
const { updateSnapshotState } = require('../src/index');
global.UNSTABLE_SKIP_REPORTING = false;
const originalState = { some: 'value' };
updateSnapshotState(originalState, { another: 'val' });
expect(originalState).toEqual({ some: 'value', another: 'val' });
});
});

@@ -32,2 +32,4 @@ /*

beforeAll(() => {
// In tests, skip reporting (skip snapshotState update to not mess with our test report)
global.UNSTABLE_SKIP_REPORTING = true;
const { toMatchImageSnapshot } = require('../src'); // eslint-disable-line global-require

@@ -34,0 +36,0 @@ expect.extend({ toMatchImageSnapshot });

2

package.json
{
"name": "jest-image-snapshot",
"version": "2.4.2",
"version": "2.4.3",
"description": "Jest matcher for image comparisons. Most commonly used for visual regression testing.",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -84,2 +84,14 @@ # jest-image-snapshot

### Recipes
#### Upload diff images from failed tests
[Example Image Upload Test Reporter](examples/image-reporter.js)
If you are using jest-image-snapshot in an ephemeral environment (like a Continuous Integration server) where the file system does not persist, you might want a way to retrieve those images for diagnostics or historical reference. This example shows how to use a custom [Jest Reporter](https://facebook.github.io/jest/docs/en/configuration.html#reporters-array-modulename-modulename-options) that will run after every test, and if there were any images created because they failed the diff test, upload those images to an [AWS S3](https://aws.amazon.com/s3/) bucket.
To enable this image reporter, add it to your `jest.config.js` "reporters" definition:
"reporters": [ "default", "<rootDir>/image-reporter.js" ]
## How it works

@@ -86,0 +98,0 @@ Given an image (Buffer instance with PNG image 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.

@@ -24,4 +24,7 @@ /*

function updateSnapshotState(oldSnapshotState, newSnapshotState) {
return merge({}, oldSnapshotState, newSnapshotState);
function updateSnapshotState(originalSnapshotState, partialSnapshotState) {
if (global.UNSTABLE_SKIP_REPORTING) {
return originalSnapshotState;
}
return merge(originalSnapshotState, partialSnapshotState);
}

@@ -31,2 +34,3 @@

customDiffConfig: commonCustomDiffConfig = {},
customSnapshotsDir: commonCustomSnapshotsDir,
noColors: commonNoColors = false,

@@ -38,3 +42,3 @@ failureThreshold: commonFailureThreshold = 0,

customSnapshotIdentifier = '',
customSnapshotsDir,
customSnapshotsDir = commonCustomSnapshotsDir,
customDiffConfig = {},

@@ -45,8 +49,10 @@ noColors = commonNoColors,

} = {}) {
const { testPath, currentTestName, isNot } = this;
const {
testPath, currentTestName, isNot, snapshotState,
} = this;
const chalk = new Chalk({ enabled: !noColors });
let { snapshotState } = this;
if (isNot) { throw new Error('Jest: `.not` cannot be used with `.toMatchImageSnapshot()`.'); }
updateSnapshotState(snapshotState, { _counters: snapshotState._counters.set(currentTestName, (snapshotState._counters.get(currentTestName) || 0) + 1) }); // eslint-disable-line max-len

@@ -88,5 +94,5 @@ const snapshotIdentifier = customSnapshotIdentifier || kebabCase(`${path.basename(testPath)}-${currentTestName}-${snapshotState._counters.get(currentTestName)}`);

// https://github.com/facebook/jest/pull/3668
snapshotState = updateSnapshotState(snapshotState, { updated: snapshotState.updated += 1 });
updateSnapshotState(snapshotState, { updated: snapshotState.updated + 1 });
} else if (result.added) {
snapshotState = updateSnapshotState(snapshotState, { added: snapshotState.added += 1 });
updateSnapshotState(snapshotState, { added: snapshotState.added + 1 });
} else {

@@ -96,2 +102,3 @@ ({ pass } = result);

if (!pass) {
updateSnapshotState(snapshotState, { unmatched: snapshotState.unmatched + 1 });
const differencePercentage = result.diffRatio * 100;

@@ -113,2 +120,3 @@ message = () => `Expected image to match or be a close match to snapshot but was ${differencePercentage}% different from snapshot (${result.diffPixelCount} differing pixels).\n`

configureToMatchImageSnapshot,
updateSnapshotState,
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc