Skip to content

Instantly share code, notes, and snippets.

@griffinmyers
Created June 26, 2017 23:16

Revisions

  1. Will Myers created this gist Jun 26, 2017.
    60 changes: 60 additions & 0 deletions diff-test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    const fs = require('fs');
    const path = require('path');

    const imageDiff = require('image-diff');

    const testCases = fs.readdirSync(path.join('/', 'visual-diff', 'results'));
    const expectations = fs.readdirSync(path.join(__dirname, 'expectations'));

    const localBasePath = path.join('test', 'visual-diff');
    const dockerBasePath = path.join('/', 'visual-diff');

    const expectedExtension = '.png';

    describe('expecation has result', function() {

    expectations.forEach(function(expectation) {
    if (expectation.endsWith(expectedExtension)) {
    it(expectation, function() {
    if (testCases.indexOf(expectation) === -1) {
    expect('No result exists for expectation ' + expectation).toBeNull();
    }
    });
    }
    });

    });

    describe('result matches expectation', function() {

    testCases.forEach(function(testCase) {
    if (testCase.endsWith(expectedExtension)) {
    const testPath = path.join(dockerBasePath, 'results', testCase);
    const expectationPath = path.join(localBasePath, 'expectations', testCase);
    const diffPath = path.join(dockerBasePath, 'diff', testCase);

    it(testCase, function(done) {
    fs.access(path.join(__dirname, 'expectations', testCase), fs.R_OK, function(err) {
    if (err) {
    expect('No expectation exists for ' + testCase).toBeNull();
    done();
    return;
    }

    imageDiff.getFullResult({
    actualImage: testPath,
    expectedImage: expectationPath,
    diffImage: diffPath,
    shadow: true
    }, function(err, result) {
    expect(err).toBeNull();
    expect(result.percentage).toBeLessThan(0.01);
    done();
    });
    });

    });
    }
    });

    });