Created
November 20, 2020 19:10
-
-
Save maksimr/c224283d59057eaf7cb8b4418a60075b to your computer and use it in GitHub Desktop.
Adopt jest-image-snapshot for jasmine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jasmine.getEnv().addReporter(new class { | |
specStarted(spec) { | |
jasmine.getEnv().currentSpec = spec; | |
} | |
specDone() { | |
jasmine.getEnv().currentSpec = null; | |
} | |
}()); | |
beforeAll(() => { | |
jasmine.addMatchers({ | |
toMatchImageSnapshot: function() { | |
const SNAPSHOTS_DIR = '__image_snapshots__'; | |
const {diffImageToSnapshot, runDiffImageToSnapshot} = require('jest-image-snapshot/src/diff-snapshot'); | |
const updateSnapshot = process.argv.find((arg) => /^\s*(--updateSnapshot|-u)\s*$/.test(arg)); | |
return { | |
compare(actual, { | |
customDiffConfig = {}, | |
customSnapshotsDir, | |
customDiffDir, | |
diffDirection = 'horizontal', | |
failureThreshold = 0, | |
failureThresholdType = 'pixel', | |
updatePassedSnapshot = false, | |
blur = 0, | |
runInProcess = false, | |
allowSizeMismatch = false, | |
comparisonMethod = 'pixelmatch' | |
} = {}) { | |
const path = require('path'); | |
const currentSpec = jasmine.getEnv().currentSpec; | |
const testPath = __filename; | |
const snapshotIdentifier = currentSpec.fullName.replace(/\s+/g, '_'); | |
const snapshotsDir = customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR); | |
const diffDir = customDiffDir || path.join(snapshotsDir, '__diff_output__'); | |
const imageToSnapshot = runInProcess ? diffImageToSnapshot : runDiffImageToSnapshot; | |
const result = imageToSnapshot({ | |
receivedImageBuffer: actual, | |
snapshotsDir, | |
diffDir, | |
diffDirection, | |
snapshotIdentifier, | |
updateSnapshot: updateSnapshot, | |
customDiffConfig: Object.assign({}, customDiffConfig), | |
failureThreshold, | |
failureThresholdType, | |
updatePassedSnapshot, | |
blur, | |
allowSizeMismatch, | |
comparisonMethod | |
}); | |
let pass = true; | |
let message = ''; | |
if (!result.updated && !result.added) { | |
({pass} = result); | |
if (!pass) { | |
const differencePercentage = result.diffRatio * 100; | |
message = (result.diffSize && !allowSizeMismatch ? | |
`Expected image to be the same size as the snapshot (${result.imageDimensions.baselineWidth}x${result.imageDimensions.baselineHeight}), but was different (${result.imageDimensions.receivedWidth}x${result.imageDimensions.receivedHeight}).\n` : | |
`Expected image to match or be a close match to snapshot but was ${differencePercentage}% different from snapshot (${result.diffPixelCount} differing pixels).\n`) | |
+ `${('See diff for details:')} ${(result.diffOutputPath)}`; | |
} | |
} | |
return { | |
message, | |
pass | |
}; | |
} | |
}; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment