Skip to content

Instantly share code, notes, and snippets.

@helephant
Created January 30, 2019 17:23
Show Gist options
  • Save helephant/805d510f027e5a88c308dc4bafc9dbd6 to your computer and use it in GitHub Desktop.
Save helephant/805d510f027e5a88c308dc4bafc9dbd6 to your computer and use it in GitHub Desktop.
How to use jest to compare an image to a pre-generated screenshot using an npm library called toMatchImageSnapshot
const { toMatchImageSnapshot } = require('jest-image-snapshot');
const path = require('path');
const fs = require('fs');
var gm = require("gm").subClass({imageMagick: true});;
jest.setTimeout(15000);
expect.extend({ toMatchImageSnapshot });
const imageAtTestPath = path.resolve(__dirname, '11c429f0-8bee-4c86-ab17-e04cf2bd2e11.jpeg');
const pngAtTestPath = path.resolve(__dirname, '11c429f0-8bee-4c86-ab17-e04cf2bd2e11.png');
test('image matches pre-generated snapshot', async () => {
const pngStream = fs.readFileSync(pngAtTestPath);
expect(pngStream).toMatchImageSnapshot();
});
test('image matches jpg snapshot', async (done) => {
gm(imageAtTestPath).toBuffer('PNG', (error, buffer) => {
expect(buffer).toMatchImageSnapshot();
done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment