Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
jest-image-snapshot
Advanced tools
Jest matcher for screenshot comparisons. Most commonly used for browser visual testing.
jest-image-snapshot is a Jest matcher for image comparisons. It allows you to write tests that compare images to a baseline image, making it useful for visual regression testing.
Basic Image Comparison
This feature allows you to compare an image to a stored snapshot. If the images do not match, the test will fail.
const { toMatchImageSnapshot } = require('jest-image-snapshot');
test('compares image to snapshot', () => {
const image = fs.readFileSync('path/to/image.png');
expect(image).toMatchImageSnapshot();
});
Custom Configuration
This feature allows you to customize the image comparison settings, such as the threshold for differences and whether to use colors in the diff output.
const { toMatchImageSnapshot } = require('jest-image-snapshot');
expect.extend({ toMatchImageSnapshot });
test('compares image with custom config', () => {
const image = fs.readFileSync('path/to/image.png');
expect(image).toMatchImageSnapshot({
customDiffConfig: { threshold: 0.1 },
noColors: true,
});
});
Update Snapshots
This feature allows you to update the stored snapshot with a new image. This is useful when the baseline image needs to be updated.
const { toMatchImageSnapshot } = require('jest-image-snapshot');
test('updates image snapshot', () => {
const image = fs.readFileSync('path/to/image.png');
expect(image).toMatchImageSnapshot({ updateSnapshot: true });
});
cypress-image-snapshot is a plugin for Cypress that provides similar functionality to jest-image-snapshot but is designed for use with Cypress end-to-end tests. It allows you to take snapshots of your application and compare them to baseline images.
puppeteer-screenshot-tester is a package that allows you to take screenshots using Puppeteer and compare them to baseline images. It is similar to jest-image-snapshot but is designed to work with Puppeteer for browser automation.
reg-suit is a visual regression testing tool that integrates with various CI/CD pipelines. It provides a comprehensive solution for visual regression testing, including image comparison, reporting, and integration with version control systems. It offers more features and integrations compared to jest-image-snapshot.
Jest matcher that performs image comparisons using Blink-diff and behaves just like Jest snapshots do! Very useful for browser visual comparison testing.
npm i --save-dev jest-image-snapshot
Please note that Jest
20.x.x is a peerDependency. jest-image-snapshot
will not work with anything below jest 20.x.x
expect
const { toMatchImageSnapshot } = require('jest-image-snapshot');
expect.extend({ toMatchImageSnapshot });
toMatchImageSnapshot()
in your tests!it('should demonstrate this matcher`s usage', () => {
...
expect(image).toMatchImageSnapshot();
});
toMatchImageSnapshot()
takes an optional options object where you can provide your own blink-diff configuration parameters and/or a custom snapshot identifier string and/or forcing no styled output for possibly storing the results in a file:
it('should demonstrate this matcher`s usage with a custom blink-diff config', () => {
...
const blinkDiffConfig = { perceptual: true };
expect(image).toMatchImageSnapshot({
customDiffConfig: blinkDiffConfig,
customSnapshotIdentifier: 'customSnapshotName',
noColors: true // the default is false
});
});
Any blink-diff custom configuration can be provided so long as the values for imageAPath
, imageA
, imageBPath
, imageB
, or imageOutputPath
are not changed as these are used internally.
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.
On subsequent test runs the matcher will compare the image being passed against the stored snapshot.
To update the stored image snapshot run jest with --updateSnapshot
or -u
argument. All this works the same way as Jest snapshots.
Typically this matcher is used to for visual tests that run on a browser. For example let's say I finish working on a feature and want to write a test to prevent visual regressions:
it('renders correctly', async () => {
const browser = await launchChromeHeadless();
await browser.goTo('https://localhost:3000');
const screenshot = await browser.takeScreenshot();
expect(screenshot).toMatchImageSnapshot();
});
Then after a few days as I finish adding another feature to my component I notice one of my tests failing!
Oh no! I must have introduced a regression! Let's see what the diff looks like to identify what I need to fix:
And now that I know that I broke the card art I can fix it!
Thanks jest-image-snapshot
, that broken header would not have looked good in production!
We welcome Your interest in the American Express Open Source Community on Github. Any Contributor to any Open Source Project managed by the American Express Open Source Community must accept and sign an Agreement indicating agreement to the terms below. Except for the rights granted in this Agreement to American Express and to recipients of software distributed by American Express, You reserve all right, title, and interest, if any, in and to Your Contributions. Please fill out the Agreement.
Please feel free to open pull requests and see CONTRIBUTING.md
for commit formatting details.
Any contributions made under this project will be governed by the Apache License 2.0.
This project adheres to the American Express Community Guidelines. By participating, you are expected to honor these guidelines.
FAQs
Jest matcher for image comparisons. Most commonly used for visual regression testing.
The npm package jest-image-snapshot receives a total of 407,080 weekly downloads. As such, jest-image-snapshot popularity was classified as popular.
We found that jest-image-snapshot demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.