|
| 1 | +--- |
| 2 | +name: visualci-laravel-rust |
| 3 | +description: "Sets up VisualCI for a Laravel Rust app's tests with the snapshot-testing-laravel crate: browser screenshots and HTML, JSON and text snapshots of responses, run under `visualci test -- cargo test`, with names, platforms, ignore rules and thresholds as VisualCI sees them. Use when a Laravel Rust project uses snapshot-testing-laravel, or wants screenshot or snapshot tests with VisualCI. Not for other Rust projects (see visualci-insta) or other frameworks." |
| 4 | +license: Apache-2.0 |
| 5 | +metadata: |
| 6 | + docs: https://visualci.com/docs/snapshots/laravel-rust.md |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- Generated from server/frontend/content/docs/snapshots/laravel-rust.md by server/frontend/scripts/skills.mjs. Edit the page. --> |
| 10 | + |
| 11 | +# Laravel Rust |
| 12 | + |
| 13 | +[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. |
| 14 | + |
| 15 | +```rust |
| 16 | +use laravel::testing::prelude::*; |
| 17 | +use snapshot_testing_laravel::prelude::*; |
| 18 | + |
| 19 | +#[laravel::test] |
| 20 | +async fn the_pricing_page_looks_right(app: Application) -> laravel::Result<()> { |
| 21 | + let browser = app.browse().await?; |
| 22 | + browser.visit("/pricing").await?.assert_see("Pro").await?; |
| 23 | + browser.assert_screenshot("pricing/desktop").await?; |
| 24 | + browser.resize(390, 844).await?.assert_screenshot("pricing/mobile").await?; |
| 25 | + Ok(()) |
| 26 | +} |
| 27 | + |
| 28 | +#[laravel::test] |
| 29 | +async fn the_api_matches_its_snapshot(app: Application) { |
| 30 | + app.get("/api/users/1") |
| 31 | + .await |
| 32 | + .assert_ok() |
| 33 | + .assert_matches_snapshot_ignoring("api/users/show", ["$.updated_at"]); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +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. |
| 38 | + |
| 39 | +## 1. Install |
| 40 | + |
| 41 | +```toml |
| 42 | +[dev-dependencies] |
| 43 | +snapshot-testing-laravel = "0.1" |
| 44 | +``` |
| 45 | + |
| 46 | +Browser tests need Chrome or Chromium. Screenshots change a little between Chrome versions, so pin one, the same locally and in CI: |
| 47 | + |
| 48 | +```sh |
| 49 | +npx playwright@1.65.2 install --with-deps --only-shell chromium |
| 50 | +``` |
| 51 | + |
| 52 | +For snapshots without a browser, `default-features = false` leaves the browser out. |
| 53 | + |
| 54 | +Then the CLI, and a project token (see [Quick start](https://visualci.com/docs/quick-start.md)): |
| 55 | + |
| 56 | +```sh |
| 57 | +curl -fsSL https://visualci.com/install.sh | VISUALCI_VERSION=0.2.10 sh |
| 58 | +export VISUALCI_TOKEN=vci_... |
| 59 | +``` |
| 60 | + |
| 61 | +## 2. Run the tests under visualci test |
| 62 | + |
| 63 | +```sh |
| 64 | +visualci test -- cargo test |
| 65 | +``` |
| 66 | + |
| 67 | +`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. |
| 68 | + |
| 69 | +- **Under `visualci test`** (in CI, or on your machine), a changed snapshot doesn't fail its test. The run ends with what changed, with links, and on a pull request the VisualCI check waits until the change is reviewed. |
| 70 | +- **Plain `cargo test`** with `VISUALCI_TOKEN` set (and no `VISUALCI_BUILD_ID`) is a local run: each test process makes a local build, and each test fails once, listing every snapshot it changed with a link to review it. `SNAPSHOT_FAIL_FAST=1` (or `VISUALCI_FAIL_FAST=1`) fails a browser test at its first changed screenshot instead. |
| 71 | + |
| 72 | +Approving a local run's change on VisualCI makes it your branch's baseline. |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +On GitHub Actions: |
| 77 | + |
| 78 | +```yaml |
| 79 | +- uses: actions/setup-node@v4 |
| 80 | + with: |
| 81 | + node-version: 22 |
| 82 | +- run: npx -y playwright@1.65.2 install --with-deps --only-shell chromium |
| 83 | +- run: | |
| 84 | + curl -fsSL https://visualci.com/install.sh | VISUALCI_VERSION=0.2.10 VISUALCI_INSTALL_DIR="$HOME/bin" sh |
| 85 | + echo "$HOME/bin" >> "$GITHUB_PATH" |
| 86 | +- run: visualci test -- cargo test --locked |
| 87 | + env: |
| 88 | + VISUALCI_TOKEN: ${{ secrets.VISUALCI_TOKEN }} |
| 89 | +``` |
| 90 | +
|
| 91 | +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)). |
| 92 | + |
| 93 | +## How snapshots appear on VisualCI |
| 94 | + |
| 95 | +- **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. |
| 96 | +- **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`). |
| 97 | +- **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)). |
| 98 | +- **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). |
| 99 | +- **`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. |
| 100 | + |
| 101 | +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). |
| 102 | + |
| 103 | +## Reviewing the changes |
| 104 | + |
| 105 | +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). |
| 106 | + |
| 107 | +This skill is the page https://visualci.com/docs/snapshots/laravel-rust.md. All of VisualCI's documentation: https://visualci.com/llms.txt |
0 commit comments