Socket
Book a DemoInstallSign in
Socket

jest-image-snapshot

Package Overview
Dependencies
Maintainers
4
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-image-snapshot

Jest matcher for screenshot comparisons. Most commonly used for browser visual testing.

Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
639K
-5.77%
Maintainers
4
Weekly downloads
 
Created
Source

jest-image-snapshot

npm version Build Status

Jest matcher that performs image comparisons using pixelmatch and behaves just like Jest snapshots do! Very useful for browser visual comparison testing.

Installation:

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

Usage:

  • Extend Jest's expect
  const { toMatchImageSnapshot } = require('jest-image-snapshot');

  expect.extend({ toMatchImageSnapshot });
  • Use toMatchImageSnapshot() in your tests!
it('should demonstrate this matcher`s usage', () => {
  ...
  expect(image).toMatchImageSnapshot();
});

Optional configuration:

toMatchImageSnapshot() takes an optional options object with the following properties:

  • customDiffConfig: Custom config passed pixelmatch (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
  • customSnapshotIdentifier: A custom name to give this snapshot. If not provided one is computed automatically
  • noColors: (default false) Removes colouring from console output, useful if storing the results in a file
  it('should demonstrate this matcher`s usage with a custom pixelmatch config', () => {
    ...
    const customConfig = { threshold: 0.5 };
    expect(image).toMatchImageSnapshot({
      customDiffConfig: customConfig,
      customSnapshotIdentifier: 'customSnapshotName',
      noColors: true // the default is false
    });
  });

Custom defaults can be set with a configurable extension. This will allow for customization of this module's defaults. For example, a 0% default threshold can be shared across all tests with the configuration below.

const { configureToMatchImageSnapshot } = require('jest-image-snapshot');

const customConfig = { threshold: 0 };
const toMatchImageSnapshot = configureToMatchImageSnapshot({ 
  customDiffConfig: customConfig, 
  noColors: true,
})
expect.extend({ toMatchImageSnapshot });

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.

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.

See it in action

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!

Contributing

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.

License

Any contributions made under this project will be governed by the Apache License 2.0.

Code of Conduct

This project adheres to the American Express Community Guidelines. By participating, you are expected to honor these guidelines.

Keywords

test

FAQs

Package last updated on 24 Oct 2017

Did you know?

Socket

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.

Install

Related posts