reg-cli: Visual Regression Test Tool
reg-cli is a command-line interface (CLI) tool for performing visual regression testing on images and generating a comprehensive, interactive HTML report. It helps developers detect unintended visual changes in user interfaces over time by comparing 'actual' images with 'expected' baseline images. The current stable version is 0.18.14, with development actively focused on minor feature enhancements to the report UI, dependency updates, and security patches. Key differentiators include its flexible image comparison logic with adjustable thresholds, support for parallel processing, and the ability to generate reports from pre-existing JSON data. It requires Node.js v18 or newer for execution, ensuring compatibility with modern JavaScript environments. It is primarily used via its CLI but also exposes a programmatic API for integration into custom build workflows.
Common errors
-
Error: Node.js version too old. `reg-cli` requires Node.js v18 or higher.
cause The installed Node.js version is below the minimum requirement (v18).fixUpdate your Node.js environment to version 18 or newer. For example, using `nvm install 18 && nvm use 18`. -
Error: No images found to compare.
cause The specified 'actual' directory is empty or contains no images matching the expected comparison format.fixEnsure that your `actual` image directory contains the images you intend to compare. Verify the paths provided to `reg-cli` are correct. -
Command failed with exit code 2 (or similar non-zero code) after image comparison.
cause By default, `reg-cli` exits with an error code if visual differences, new images, or deleted images are detected.fixIf this is an expected outcome (e.g., in a CI pipeline where differences are allowed but reported), add the `--ignoreChange` flag to the `reg-cli` command. Use `--extendedErrors` in addition to `--ignoreChange` if you also want to ignore errors for added/deleted images. -
Error: Argument 'threshold' can't be used anymore. Use 'thresholdRate' or 'thresholdPixel'.
cause You are using the deprecated `--threshold` option in a version where it has been removed or strictly aliased.fixReplace `--threshold` with `--thresholdRate` (e.g., `--thresholdRate 0.05`) or `--thresholdPixel` (e.g., `--thresholdPixel 10`) depending on your desired comparison metric.
Warnings
- breaking The `--threshold` option was deprecated and replaced by `--thresholdRate` and `--thresholdPixel` to provide more granular control over change detection.
- breaking Node.js v18 or higher is now required. Older Node.js versions are no longer supported, which may cause installation or runtime errors.
- gotcha By default, `reg-cli` will exit with a non-zero status code if image changes are detected (diff images are created).
- gotcha reg-cli switched its underlying image difference library from `image-diff` to `argos-ci/image-difference` due to `image-diff` no longer being maintained. This change may subtly affect comparison results.
- gotcha Recent versions include security updates for dependencies like `serve-static` and `tar`. Running older versions might expose your project to known vulnerabilities.
Install
-
npm install reg-cli -
yarn add reg-cli -
pnpm add reg-cli
Imports
- RegCli
import { RegCli } from 'reg-cli';import RegCli from 'reg-cli';
- RegCliOptions
import { RegCliOptions } from 'reg-cli';import type { RegCliOptions } from 'reg-cli'; - RegResult
import { RegResult } from 'reg-cli';import type { RegResult } from 'reg-cli';
Quickstart
import { spawn } from 'node:child_process';
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
const actualDir = './screenshots/actual';
const expectedDir = './screenshots/expected';
const diffDir = './screenshots/diff';
const reportPath = './reg-report/index.html';
async function setupDirs() {
await mkdir(actualDir, { recursive: true });
await mkdir(expectedDir, { recursive: true });
await mkdir(diffDir, { recursive: true });
// Simulate creating a dummy actual image
await writeFile(join(actualDir, 'test.png'), Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64'));
// Simulate creating a dummy expected image for comparison
await writeFile(join(expectedDir, 'test.png'), Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64'));
}
async function runRegCli() {
await setupDirs();
const args = [
actualDir,
expectedDir,
diffDir,
'-R', reportPath,
'--matchingThreshold', '0.01',
'--thresholdRate', '0.001'
];
const regCliProcess = spawn('npx', ['reg-cli', ...args], { stdio: 'inherit' });
regCliProcess.on('close', (code) => {
if (code === 0) {
console.log('reg-cli comparison completed successfully. Check report at', reportPath);
} else {
console.error(`reg-cli exited with code ${code}. Visual differences might have been found.`);
}
});
}
runRegCli().catch(console.error);