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

mocha-image-compare

v1.0.3

Published

Compares given image with provided buffer or file

Downloads

479

Readme

mocha-image-compare

Module makes possible to compare images during mocha test execution. It generates report in specified folder which includes provided test image, image received from buffer/file, diff image (if previous tests were ok) and report json file for each test.

It compares images by three parameters:

  • format (PNG, JPG etc)
  • geometry (width/height)
  • content (by MSE metric)

Installation

npm install --save-dev mocha-image-compare

Usage

This module is pretty easy to use. First include it into you test file:

var mic = require('mocha-image-compare');

Also, you can specify optional parameters like

var mic = require('mocha-image-comapre')({
  report: '/path/to/report/folder', // path to the report folder, default
                                    // is './report'
  threshold: 0.002, // default detection thresold, default is 0.001
  highlight: 'yellow' // image diff highlight color
});

Please note, this way you'll have common scope for all tests run in common, even if you include it in other test file.

To have a separate scope you need to instantiate a new instance, like this:

var scope = new mic(options);

To get actual compare function you need to bind test with the comparator instance like

it('my cool test', function(done) {
  var compare = mic.test(this);
  // other stuff
  compare('/path/to/file/to/compare/with.jpg', buffer, done);
});

You can test an already saved file too:

it('my cool test', function(done) {
  var compare = mic.test(this);
  // other stuff
  compare('/file/to/compare/with.jpg', 0.004, // you can specify threshold too!
          '/test/file/to/compare.jpg', done);
});

There is also a syntax sugar which will be pretty useful for testing with supertest library:

it('my cool test', function(done) {
  var compare = mic.test(this);
  // other stuff
  api.get('/')
     .expect('Content-Type': 'image/jpeg')
     .end(compare.asSupertest('/file/to/compare/with.jpg', done));
});

Report

Report folder contains up to four files per compare invocation:

  1. <sha1-hash>-src.<filetype>: original file
  2. <sha1-hash>-dst.<filetype>: resulting file saved from buffer/file
  3. <sha1-hash>-diff.png: image shows visual difference between images
  4. <sha1-hash>-report.json: JSON encoded test results.

Example JSON report is:

{
  // threshold for test
  "threshold": 0.001,
  "test": "/project/test/001-thumbnails.js", // test suite file
  "name": [
    "thumbnails", // suite name
    "should create thumbnail with scaling" // test name
  ],
  // original file
  "src": "./report/1b924ae63414b5876a0b04feeaa88d41b0d2da41-src.png",
  // report file (this one!)
  "report": "./report/1b924ae63414b5876a0b04feeaa88d41b0d2da41-report.json",
  // object dimensions
  "geometry": {
    "src": "200x82",
    "dst": "200x82"
  },
  // object formats
  "format": {
    "src": "PNG (Portable Network Graphics)",
    "dst": "PNG (Portable Network Graphics)"
  },
  // got file/content
  "dst": "./report/1b924ae63414b5876a0b04feeaa88d41b0d2da41-dst.png",
  // image diff file
  "diff": "./report/1b924ae63414b5876a0b04feeaa88d41b0d2da41-diff.png",
  // actual equality value
  "equality": 0,
  // content comparison test result
  "isEqual": true
}