Visual Regression Tests Across Branches: How-To
In this article
Visual regression testing compares screenshots of your UI before and after code changes to catch unintended visual breakage. Running these tests across multiple branches lets you verify each feature branch independently without waiting for a merge. This guide walks you through setting up and running visual regression tests on multiple branches using a Git-based workflow, assuming you already have a visual testing tool (like Percy, Chromatic, or BackstopJS) integrated with your project.
What You'll Need
- A Git repository with at least two branches (e.g.,
mainand a feature branch). - A visual regression testing tool configured (e.g., Percy, Chromatic, or a local tool like BackstopJS).
- Access to a CI/CD platform (GitHub Actions, GitLab CI, CircleCI, etc.) or a local terminal with Git.
- Node.js and npm/yarn installed if using JavaScript-based tools.
- Basic familiarity with Git commands and your CI platform's YAML syntax.
Step-by-Step
- Set up your visual testing tool in the repository. Install the SDK/CLI for your chosen tool. For example, with Percy:
npm install --save-dev @percy/cli. Add the necessary configuration file (e.g.,percy.config.jsorbackstop.json) that defines the URLs or storybook stories to snapshot. - Create a baseline branch. The baseline is the reference for comparison. Typically, this is your
mainbranch. Ensure it has a stable, reviewed UI. Run your visual tests on this branch and approve all snapshots as the baseline. - Configure CI to run tests on every branch. In your CI configuration (e.g., GitHub Actions), add a job that runs on
pull_requestevents and pushes to feature branches. The job should check out the code, install dependencies, build the app, and run the visual test command (e.g.,percy exec -- npm test). - Set up branch-specific environment variables. Most visual testing services require a project token. Store it as a secret in your CI. For local testing, you can set it in your shell. Ensure the token is scoped to the correct project so that branches are compared against the right baseline.
- Run the tests on a feature branch. Push a change to a feature branch or open a pull request. The CI job will run the visual tests, take screenshots, and upload them to your visual testing service. The service will compare them against the baseline from
main. - Review the visual diffs. Open the visual testing dashboard (e.g., Percy's UI). You'll see a list of snapshots with any pixel differences highlighted. Review each diff to determine if it's an intended change or a bug.
- Approve or reject changes. In the dashboard, approve snapshots that are correct. If there are unintended changes, fix the code and push again. The CI will re-run and upload new snapshots for comparison.
- Handle multiple branches simultaneously. Since each branch runs independently, you can have several feature branches in flight. The service will track each branch's snapshots separately, all against the same baseline. This allows parallel development without interference.
- Update baseline when merging to main. After merging a feature branch, the baseline on
mainshould be updated. Run the visual tests onmainafter the merge and approve any new snapshots. This ensures the next branches compare against the latest stable UI.
Common Mistakes
- Not updating the baseline after merging. If you don't re-baseline
main, future branches will compare against an outdated reference, causing false positives. - Using a single token for multiple projects. Each project should have its own token. Using the wrong token can cause tests to be uploaded to the wrong project, making comparisons meaningless.
- Ignoring environment-specific differences. If your CI runs on different operating systems or browsers than your local machine, screenshots may differ due to font rendering or browser versions. Use consistent environments across branches.
- Running tests on only the default branch. If you only run on
main, you miss catching regressions before merge. Always run on feature branches. - Not setting a stable baseline. If your
mainbranch is frequently broken or has unapproved changes, the baseline becomes unreliable. Keepmainstable.
Tips
- Use a dedicated baseline branch. Some teams create a
visual-baselinebranch that only changes when the UI is intentionally updated. This prevents accidental baseline shifts. - Automate approval for trivial changes. Some tools allow you to set thresholds for pixel differences. Use this to auto-approve minor changes like anti-aliasing variations.
- Run visual tests only on relevant paths. If your project has a large UI, limit snapshots to components that actually changed to save time and reduce noise.
- Integrate with your code review process. Use CI status checks to block merging if visual tests fail or have unapproved diffs.
- Keep your test suite fast. Use parallelization and caching to ensure visual tests don't become a bottleneck in your CI pipeline.
Troubleshooting
Tests fail to run on a branch. Check the CI logs for missing dependencies or build errors. Ensure your build command is correct and that the branch has the latest code from main merged in.
No snapshots appear in the dashboard. Verify that the visual test command actually runs and that the token is correctly set. Also check that the test script is not being skipped due to conditions in your CI config.
Diffs show false positives. This often happens due to environment differences (e.g., fonts, browser versions). Use a Docker container in CI to standardize the environment. Also, check for dynamic content like dates or animations that cause flaky snapshots.
Baseline is not updating after merge. Ensure that the CI job on main runs the visual tests and that you approve the new snapshots. Some tools require manual approval before the baseline is updated.
Branch tests are comparing against the wrong baseline. Make sure the token is scoped to the correct project. If you have multiple projects, each branch must use the token for the project that contains the baseline.
Frequently Asked Questions
Q: Can I run visual tests locally on multiple branches?
A: Yes. You can run them locally by checking out each branch and running the test command. However, you'll need to manage the baseline manually, as most services expect CI to upload snapshots.
Q: How do I handle visual changes that are intentional?
A: Approve the snapshots in the visual testing dashboard. If you're using a service like Percy, you can approve individual snapshots or the entire build.
Q: What if I have many feature branches open?
A: That's fine. Each branch runs independently and compares against the same baseline. You can review each branch's diffs separately.
Q: Do I need to run visual tests on every push?
A: Not necessarily. You can configure CI to run on pull requests only, or on specific paths. This reduces CI load.
Q: How do I update the baseline after a major UI overhaul?
A: Merge the changes to main, run the visual tests on main, and approve all snapshots. This sets a new baseline for future branches.
Q: Can I use visual regression tests with a monorepo?
A: Yes, but you'll need to configure the test tool to only snapshot the relevant packages. Most tools allow you to define specific URLs or stories.