# RSpec feature specs

> Compare screenshots from RSpec feature and system specs, and PDFs, JSON, HTML and text, with the visualci gem's matchers.

Source: https://visualci.com/docs/screenshots/rspec/
All of VisualCI's documentation: https://visualci.com/llms.txt

The `visualci` gem queues snapshots during an RSpec run and uploads them with the CLI when the run ends:

```ruby
# Gemfile
gem 'visualci', group: :test
```

```ruby
# spec_helper.rb
require 'visualci/rspec'
RSpec.configure { |c| c.include VisualCI::RSpec }

# in a spec
expect_image_to_match_visual('signup_form/dark/1280', screenshot_path, threshold: 0.1)
expect_pdf_to_match_visual('invoice_typical', pdf_path)
expect_json_to_match_snapshot('api/users/show', JSON.parse(response.body), ignore: ['$.updated_at'])
expect_html_to_match_snapshot('emails/welcome', mail.html_part.body.to_s)
expect_text_to_match_snapshot('reports/monthly', report.to_s)
```

A feature spec takes the screenshot with Capybara first:

```ruby
it 'signs up' do
  visit '/signup'
  path = Rails.root.join('tmp/screenshots/signup_form.png').to_s
  page.save_screenshot(path)
  expect_image_to_match_visual('signup_form/1280', path, threshold: 0.1)
end
```

`ignore:` on the text, JSON and HTML helpers lists values that change on every run, for that snapshot only: JSON paths (strings starting with `$`) and regular expressions. Rules for many snapshots belong in [`visualci.yml`](https://visualci.com/docs/reference/config.md).

In a local run (no `VISUALCI_BUILD_ID`), each example's snapshots are compared when the example ends. An example that changed any fails once, listing every changed snapshot with its link, so a long feature spec shows all its changes in one run:

```text
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/…
```

Set `VISUALCI_FAIL_FAST=1` to fail at the first changed snapshot instead.

In CI the helpers don't fail the example. Changes are reviewed on VisualCI, where the GitHub check waits; a project in `strict` mode fails the CI step instead, with `visualci build finalize --wait`.

They're on when `VISUALCI_TOKEN` is set (and `VISUALCI_DISABLED` isn't). The build comes from `VISUALCI_BUILD_ID`. In CI, create the build once before the shards run:

```sh
export VISUALCI_BUILD_ID=$(visualci build create --shards "$CIRCLE_NODE_TOTAL")
bundle exec rspec    # each shard uploads and finalizes its own snapshots at exit
```

Each shard finalizes the shard named by `VISUALCI_SHARD`, or `CIRCLE_NODE_INDEX`. Without `VISUALCI_BUILD_ID`, the helpers create a local build. Approving a change in it sets the baseline for your branch in your environment (`local-mac` or `local-linux`), and CI keeps its own set of screenshots. A Linux machine whose screenshot exactly matches an approved Mac one passes.
