# Laravel Rust

> Screenshot and snapshot tests for Laravel Rust apps with snapshot-testing-laravel, reviewed on VisualCI instead of committed as files.

Source: https://visualci.com/docs/snapshots/laravel-rust/
All of VisualCI's documentation: https://visualci.com/llms.txt

[snapshot-testing-laravel](https://github.com/DocSpring/snapshot-testing-laravel-rs) is snapshot testing for [Laravel Rust](https://laravel.com/rust) apps: browser screenshots, plus HTML, JSON and text snapshots of your responses. On its own it keeps baselines as files in your repository. With `VISUALCI_TOKEN` set, it sends every snapshot to VisualCI instead, through the `visualci` CLI, and changes wait for review there.

```rust
use laravel::testing::prelude::*;
use snapshot_testing_laravel::prelude::*;

#[laravel::test]
async fn the_pricing_page_looks_right(app: Application) -> laravel::Result<()> {
    let browser = app.browse().await?;
    browser.visit("/pricing").await?.assert_see("Pro").await?;
    browser.assert_screenshot("pricing/desktop").await?;
    browser.resize(390, 844).await?.assert_screenshot("pricing/mobile").await?;
    Ok(())
}

#[laravel::test]
async fn the_api_matches_its_snapshot(app: Application) {
    app.get("/api/users/1")
        .await
        .assert_ok()
        .assert_matches_snapshot_ignoring("api/users/show", ["$.updated_at"]);
}
```

The crate's [README](https://github.com/DocSpring/snapshot-testing-laravel-rs#readme) covers writing the tests: browser steps, element screenshots, the snapshot assertions and the example app. This page covers running them with VisualCI.

## 1. Install

```toml
[dev-dependencies]
snapshot-testing-laravel = "0.1"
```

Browser tests need Chrome or Chromium. Screenshots change a little between Chrome versions, so pin one, the same locally and in CI:

```sh
npx playwright@1.65.2 install --with-deps --only-shell chromium
```

For snapshots without a browser, `default-features = false` leaves the browser out.

Then the CLI, and a project token (see [Quick start](https://visualci.com/docs/quick-start.md)):

```sh
curl -fsSL https://visualci.com/install.sh | VISUALCI_VERSION=0.2.12 sh
export VISUALCI_TOKEN=vci_...
```

## 2. Run the tests under visualci test

```sh
visualci test -- cargo test
```

`cargo test` runs each file in `tests/` as its own process. [`visualci test`](https://visualci.com/docs/reference/cli.md#visualci-test) creates one build for the whole run, so every one of them adds to it, and finalizes the build when `cargo test` ends, even when tests fail. It exits with `cargo test`'s status when that failed.

- **In CI**, a changed snapshot doesn't fail its test: the pull request's VisualCI check waits until the change is reviewed.
- **On your machine**, each test fails once, listing every snapshot it changed with a link to review it. Approving it there makes it your branch's baseline. `SNAPSHOT_FAIL_FAST=1` (or `VISUALCI_FAIL_FAST=1`) fails a browser test at its first changed screenshot instead.

The crate tells the two apart by `VISUALCI_MODE` (`local` or `ci`), which `visualci test` sets from CLI 0.2.12 on, and otherwise by whether `CI` is set. Plain `cargo test` with `VISUALCI_TOKEN` set works too, but each test process then makes its own local build: `visualci test` puts the whole run in one.

With `VISUALCI_TOKEN` set, nothing is written to `tests/__snapshots__` or `tests/__screenshots__`: the baselines live on VisualCI. If you kept them in git before, remove those folders. The first build becomes the baseline, or in a project that already has builds, the snapshots show as added once, to approve. `VISUALCI_DISABLED=1` compares with the files again, without sending anything.

On GitHub Actions:

```yaml
- uses: actions/setup-node@v4
  with:
    node-version: 22
- run: npx -y playwright@1.65.2 install --with-deps --only-shell chromium
- run: |
    curl -fsSL https://visualci.com/install.sh | VISUALCI_VERSION=0.2.12 VISUALCI_INSTALL_DIR="$HOME/bin" sh
    echo "$HOME/bin" >> "$GITHUB_PATH"
- run: visualci test -- cargo test --locked
  env:
    VISUALCI_TOKEN: ${{ secrets.VISUALCI_TOKEN }}
```

The crate runs `visualci` from the `PATH` (or `VISUALCI_CLI`). Check out with `fetch-depth: 0` so VisualCI can find each commit's baseline (see [GitHub Actions](https://visualci.com/docs/ci/github-actions.md)).

## How snapshots appear on VisualCI

- **Names** are the names in your tests, as they are: `assert_screenshot("settings/billing/390-dark")` is `settings/billing/390-dark` on VisualCI. Keep them stable and readable, never a counter or a timestamp.
- **Text snapshots** (HTML, JSON and text) have no platform. VisualCI compares HTML with attributes sorted and JSON with keys sorted, as the crate does, and shows JSON changes by path (`items[3].price: 10 → 12`).
- **Screenshots** carry the crate's platform, `chromium-linux`, `chromium-mac` or `chromium-windows` (`SNAPSHOT_PLATFORM` or `BrowserOptions::platform` sets another), so each platform keeps its own baselines. VisualCI also keeps separate sets for CI and for local runs on a Mac or Linux (see [Local runs](https://visualci.com/docs/local-runs.md#environments)).
- **Ignore rules** given in a test (`.ignoring(["$.created_at", r"ord_[0-9a-z]{24}"])`, rules starting with `$` are JSON paths and the rest regular expressions) go with that snapshot, and VisualCI applies them before comparing. Rules for many snapshots at once go in [`visualci.yml`](https://visualci.com/docs/reference/config.md).
- **`BrowserOptions::threshold(0.1)`** goes with each screenshot as its own threshold, VisualCI's perceptual colour threshold. Whether anti-aliased pixels count follows the project's setting.

For screenshots that don't change on every run (fixed fake data, a frozen clock with `app.travel_to(..)`, no fonts or images from other sites), see [Deterministic screenshots](https://visualci.com/docs/screenshots/deterministic.md).

## Reviewing the changes

When something changed, your coding agent reads it over VisualCI's MCP server (`get_build`, then `get_snapshot`) or with `visualci build show`, checks it against the code, and approves it or fixes the code. See [Coding agents](https://visualci.com/docs/agents.md).
