npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@morgs32/firebase-rules-snapshot

v2.8.5

Published

Jest matcher for firebase rules testing

Downloads

16

Readme

firebase-rules-snapshot

npm version

Jest testing tool for firebase using the firebase/firestore emulator server

Installation:

npm i --save-dev firebase-rules-snapshot

Please note that Jest >=20 <=24 is a peerDependency. firebase-rules-snapshot will not work with anything below Jest 20.x.x

Usage:

  1. Extend Jest's expect
const { toMatchRulesSnapshot} = require('firebase-rules-snapshot');
expect.extend({ toMatchRulesSnapshot });
  1. Use toMatchRulesSnapshot() in your tests!
it('should demonstrate this matcher`s usage', () => {
  ...
  expect().toMatchRulesSnapshot();
});

See the examples for more detailed usage or read about an example use case in the American Express Technology Blog

Optional configuration:

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

  • customDiffConfig: Custom config passed to 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.
    • 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: A custom absolute path of a directory to keep this snapshot in
  • customDiffDir: A custom absolute path of a directory to keep this diff in
  • customSnapshotIdentifier: A custom name to give this snapshot. If not provided one is computed automatically
  • diffDirection: (default: horizontal) (options horizontal or vertical) Changes diff image layout direction
  • noColors: (default false) Removes colouring from console output, useful if storing the results in a file
  • failureThreshold: (default 0) Sets the threshold that would trigger a test failure based on the failureThresholdType selected. This is different to the customDiffConfig.threshold above, that is the per pixel failure threshold, this is the failure threshold for the entire comparison.
  • failureThresholdType: (default pixel) (options percent or pixel) Sets the type of threshold that would trigger a failure.
  • updatePassedSnapshot: (default false) Updates a snapshot even if it passed the threshold against the existing one.
  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 failure threshold can be set in percent, in this case if the difference is over 1%.

  it('should fail if there is more than a 1% difference', () => {
    ...
    expect(image).toMatchImageSnapshot({
      failureThreshold: '0.01',
      failureThresholdType: 'percent'
    });
  });

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 });

jest.retryTimes()

Jest supports automatic retries on test failures. This can be useful for browser screenshot tests which tend to have more frequent false positives. Note that when using jest.retryTimes you'll have to use a unique customSnapshotIdentifier as that's the only way to reliably identify snapshots.

Recipes

Upload diff images from failed tests

Example Image Upload Test Reporter

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 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 bucket.

To enable this image reporter, add it to your jest.config.js "reporters" definition:

"reporters": [ "default", "<rootDir>/image-reporter.js" ]

Usage in TypeScript

In TypeScript, you can declare toMatchRulesSnapshot like this:

declare global {
  namespace jest {
    interface Matchers<R> {
      toMatchRulesSnapshot(): R
    }
  }
}

How it works

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.

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 page = await browser.newPage();
    await page.goto('https://localhost:3000');
    const image = await page.screenshot();

    expect(image).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

Please feel free to open pull requests.

License

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

Code of Conduct

Don't be an asshole