Write your own converter
A converter reads what a tool leaves behind (image folders, a JSON report, or both) and returns a manifest. Start with a script for your own project. If other people use the tool, turn it into a converter in the package afterward.
1. Find out what the tool writes
Run the tool with one passing comparison, one failing and one without a baseline, and look at the files. You need answers to these:
| Question | Why it matters |
|---|---|
| Where do the baseline, the actual and the diff image go? | They are the three paths of every entry. |
| Does a passing comparison leave an actual image? | Some tools clean up when it passes, some never write one. A passed entry lists no actual and no diff image. |
| What does a missing baseline look like, and what does the tool do about it? | Without a baseline the status is missing-baseline (the tool was told not to create one) or created (it wrote one). |
| Is there a report, and does it carry the diff ratio? | Without one, the status comes from which files exist and diffRatio stays unknown. |
| What were the limits? | The files rarely say. The ratio you accept (maxDiffPixelRatio, failureThreshold, ...) and the per-pixel color tolerance come from your configuration, so take them as options. |
2. Write a script
For a tool that leaves a baseline and, on failure, an actual and a diff image, fromImageTriples needs only the baselines. It infers the status from which files exist and reads the image sizes from the PNG headers:
import fs from 'fs';
import {
fromImageTriples,
writeManifestFile,
} from 'visual-regression-manifest';
const baselines = fs
.readdirSync('shots')
.filter((file) => file.endsWith('.png') && !/\.(actual|diff)\.png$/.test(file));
const manifest = fromImageTriples({
projectRoot: process.cwd(),
runner: { name: 'my-tool', version: '1.4.0' }, // the tool you convert from
triples: baselines.map((file) => ({
baseline: `shots/${file}`,
// actual and diff default to `.actual` and `.diff` siblings; pass them when yours are elsewhere
maxDiffRatio: 0.01, // your threshold, when the tool takes one
})),
});
writeManifestFile('visual-regression-manifest.my-tool.json', manifest);
When the tool has a report, or the layout needs more than the defaults, use ManifestBuilder directly, one record call per comparison:
import fs from 'fs';
import { ManifestBuilder, writeManifestFile } from 'visual-regression-manifest';
const report = JSON.parse(fs.readFileSync('my-tool-report.json', 'utf8'));
const builder = new ManifestBuilder({
projectRoot: process.cwd(),
runner: { name: 'my-tool', version: report.version },
});
for (const result of report.results) {
builder.record({
baselinePath: result.expected,
actualPath: result.received,
diffPath: result.diff ?? null, // null when the tool writes no diff image
name: result.name,
testFile: result.file,
status: result.ok ? 'passed' : 'failed',
diffRatio: result.mismatch, // a 0..1 share of the pixels, not a percentage
});
}
writeManifestFile('visual-regression-manifest.my-tool.json', builder.toJSON());
The builder makes every path relative to projectRoot, sets a path to null when the file does not exist, hashes the images that do, and keeps summary up to date. tool defaults to this library and runner is the tool you convert from.
3. Get the statuses right
Most of a converter's value is in the status, so claim only what the files show:
- A baseline the run just wrote is not a failure. If the tool creates a missing baseline, the entry is
createdand no human is needed. If it fails on a missing baseline (a CI mode), it ismissing-baseline. When the files look the same in both cases, take aupdateorcioption from the user rather than guessing. - A baseline with no actual image was not compared. Leave it out and say so under
extra, as the reg-cli, Lost Pixel and Loki converters do. - The manifest stores ratios as a share from 0 to 1. If a tool reports 2.3, store 0.023. If it reports a square root or another scale, convert it back and keep the tool's number under
extra. - Limits are optional. Leave
maxDiffRatioout when you do not know it. A missing limit means "unknown", not "zero". A per-pixel color tolerance needs acolorMetric, since a number on another scale is not comparable.
4. Check the result
import { validateManifest } from 'visual-regression-manifest';
console.log(validateManifest(manifest)); // [] when it is valid
Then look at it. Put the manifest next to the images and open the folder with the viewer:
npx visual-regression-viewer .
Every failed entry should show its three images and the slider. A baseline that shows as missing when the file exists points at a wrong path.
5. Contribute it
When the tool is used by others, a converter in the package spares each of them the script. The repository asks for:
- The converter: one file in
packages/visual-regression-manifest/src/convert/, exported fromsrc/index.ts.jest-image-snapshot.tsis the most complete one to copy. - An example project in
examples/converters/<tool>/that runs the real tool in a browser: records baselines, makes one comparison fail, removes one baseline, converts and checks the manifest against the files on disk. It captures what the tool wrote into fixtures for the unit test, so the converter is tested against real output.examples/converters/README.mdexplains the harness. - A unit test that runs the converter on those fixtures.
- A command line entry in
src/cli/convert.ts, sonpx visual-regression-manifest convert <tool>works, and a section in the package README's converting chapter, which this site's converting page is generated from. - A comparison entry for the tool in
apps/site/data/tools/<tool>.yml, with a source URL for every claim.
CONTRIBUTING has the pull request conventions. If you would rather not write it yourself, open an issue with the converter request form and a small sample of the tool's real output.