Local runs

Snapshot files and expected images don't belong in git: VisualCI keeps the baselines. A test run on your laptop compares against them, as CI does.

How a local run works

Outside CI, visualci build create makes a local build. It records the branch, your user and machine, and whether the working tree has uncommitted changes. Local builds get no GitHub check and never send notifications.

Each snapshot is compared with its own baseline: the one approved at the nearest ancestor commit, else your branch's latest, else the default branch's. So a rebased or force-pushed branch still finds the baselines approved on it.

Environments

Browsers and simulators draw a little differently on a Mac, on Linux and in your CI image, so screenshots keep a baseline set per environment: ci, local-mac and local-linux. The CLI picks the environment itself (ci when it finds a CI provider or CI=true, otherwise your operating system). VISUALCI_ENVIRONMENT sets another.

  • A screenshot is compared with its own environment's baseline. With none yet, it uses the closest: local-linux falls back to ci, then local-mac.
  • A screenshot that differs from its own baseline but exactly matches an approved image from another environment passes. The snapshot shows "matched Mac", for example. A Linux machine that draws the same pixels as your Mac doesn't need a second set.
  • PDFs are rendered on VisualCI's servers, and text and JSON are the same everywhere, so they keep one shared set.

Approving a local change

Open the build from the link the run prints and approve the change. It becomes the baseline for your branch:

  • screenshots, in your environment only. CI keeps its own set;
  • PDFs and text, everywhere. CI on the commit you push matches it with no second review.

Every change in a test, in one run

A long feature spec takes many screenshots. Failing at the first change means one fix and rerun per screenshot. Instead, record each comparison, and fail the test once at the end with every changed snapshot and its link:

VisualCI: 2 snapshots changed in this test. Approve on VisualCI if the change is right:
  signup_form/dark/1280 (local-mac) changed: https://app.visualci.com/acme/web/builds/42/snapshots/…
  signup_form/dark/390 (local-mac) changed: https://app.visualci.com/acme/web/builds/42/snapshots/…

visualci compare <names...> --wait does the comparison for any test runner: it compares those snapshots of the build now, prints a line for each changed or new one and exits 1. The examples below create a local build once per run, send each snapshot as the test makes it, and compare at the end of the test.

RSpec

The Ruby helpers do this already in local runs. Set VISUALCI_FAIL_FAST=1 to fail at the first change instead.

Jest and Vitest

// visualci.js
import { execFileSync, spawnSync } from 'node:child_process'

process.env.VISUALCI_BUILD_ID ||= execFileSync('visualci', ['build', 'create', '--subset']).toString().trim()
let names = []

/** Send a screenshot or file now; it's compared when the test ends. */
export function visual(name, path) {
  execFileSync('visualci', ['snapshot', name, path])
  names.push(name)
}

afterEach(() => {
  const sent = names
  names = []
  if (sent.length === 0) return
  const result = spawnSync('visualci', ['compare', '--wait', ...sent])
  if (result.status === 1) throw new Error(`VisualCI: changed in this test:\n${result.stdout}`)
})

Load it with setupFiles (Jest) or setupFiles in vitest.config (Vitest), and finalize the build after the run with visualci build finalize.

pytest

# conftest.py
import os, subprocess, pytest

@pytest.fixture(scope="session", autouse=True)
def visualci_build():
    if not os.environ.get("VISUALCI_BUILD_ID"):
        out = subprocess.run(["visualci", "build", "create", "--subset"], capture_output=True, text=True, check=True)
        os.environ["VISUALCI_BUILD_ID"] = out.stdout.strip()
    yield
    subprocess.run(["visualci", "build", "finalize"], check=True)

@pytest.fixture
def visual():
    names = []
    def check(name, path):
        subprocess.run(["visualci", "snapshot", name, str(path)], check=True)
        names.append(name)
    yield check
    if names:
        result = subprocess.run(["visualci", "compare", "--wait", *names], capture_output=True, text=True)
        if result.returncode == 1:
            pytest.fail("VisualCI: changed in this test:\n" + result.stdout, pytrace=False)

Rust

A guard that compares when the test's scope ends:

struct Visual(Vec<String>);

impl Visual {
    fn check(&mut self, name: &str, path: &str) {
        let status = std::process::Command::new("visualci").args(["snapshot", name, path]).status().unwrap();
        assert!(status.success(), "visualci snapshot failed");
        self.0.push(name.to_string());
    }
}

impl Drop for Visual {
    fn drop(&mut self) {
        if self.0.is_empty() || std::thread::panicking() {
            return;
        }
        let out = std::process::Command::new("visualci").args(["compare", "--wait"]).args(&self.0).output().unwrap();
        if out.status.code() == Some(1) {
            panic!("VisualCI: changed in this test:\n{}", String::from_utf8_lossy(&out.stdout));
        }
    }
}

Create the build before cargo test (export VISUALCI_BUILD_ID=$(visualci build create --subset)) and finalize it afterwards.