Cypress Screenshot Comparison in Visual Regression Tests
In this article
Visual regression testing with Cypress catches unintended UI changes by comparing screenshots of your app before and after code changes. This guide walks you through setting up and running screenshot comparisons using the popular cypress-image-diff plugin, which integrates directly with Cypress's built-in screenshot capabilities. You'll learn how to capture baseline screenshots, run comparisons, and handle failures gracefully.
What You'll Need
- A working Cypress project (Cypress 10 or later recommended)
- Node.js installed (v16 or later)
- Basic familiarity with Cypress test structure
- Access to install npm packages
Step-by-Step
- Install the plugin
Runnpm install --save-dev cypress-image-diffin your project root. This plugin adds thecompareScreenshotscommand and manages baseline images. - Configure Cypress to use the plugin
In yourcypress.config.js(orcypress.config.ts), add the plugin setup and register the task. For Cypress 10+:const { defineConfig } = require('cypress'); const { addCypressImageDiffPlugin } = require('cypress-image-diff'); module.exports = defineConfig({ e2e: { setupNodeEvents(on, config) { addCypressImageDiffPlugin(on, config); return config; }, }, }); - Import the command in your support file
Incypress/support/e2e.js(orindex.jsfor older setups), add:import 'cypress-image-diff'; - Write a test that captures a screenshot
In your test, navigate to the page you want to test and usecy.screenshot()to capture the current state. For example:it('should match the baseline for the home page', () => { cy.visit('/'); cy.screenshot('home-page'); }); - Generate the baseline screenshots
Run your test once with the environment variableCYPRESS_UPDATE_SNAPSHOTS=true. This tells the plugin to save the captured screenshots as the baseline. For example:CYPRESS_UPDATE_SNAPSHOTS=true npx cypress run. The baselines are stored incypress/snapshots/by default. - Run the comparison test
Now run the test normally (without the update flag). The plugin will compare the new screenshot against the baseline. If they differ, the test fails and the plugin saves the diff image incypress/snapshots/__diff_output__/. - Review the diff output
Open the diff image to visually inspect what changed. The plugin highlights differences in red, making it easy to spot unintended changes. - Update the baseline intentionally
If the changes are expected (e.g., you redesigned a button), re-run withCYPRESS_UPDATE_SNAPSHOTS=trueto overwrite the baseline with the new screenshot.
Common Mistakes
- Forgetting to set the update flag on the first run – Without it, the plugin will fail because no baseline exists.
- Ignoring dynamic content – Timestamps, random data, or animations cause false failures. Use Cypress's
cy.clock()or freeze animations before capturing. - Using the wrong viewport size – If your test runs at a different viewport than the baseline, the screenshots won't match. Set the viewport explicitly in the test or in the config.
- Running tests in parallel with different environments – Differences in OS rendering or fonts can cause mismatches. Use a consistent environment or Docker.
Tips
- Keep baseline images in version control so you can track when they change.
- Use a dedicated directory for visual tests and run them separately from unit tests to save time.
- Set a low failure threshold (e.g., 0.1% pixel difference) to ignore minor anti-aliasing variations.
- Use
cy.screenshot('name', { capture: 'fullPage' })to capture the entire page, not just the viewport. - For complex apps, consider using a visual regression service like Percy or Applitools, which offer more advanced features like cross-browser testing and AI-based diffing.
Troubleshooting
- Test fails with "No baseline found" – Run with
CYPRESS_UPDATE_SNAPSHOTS=trueto generate the baseline. - Diff images are all red – Check if the page has dynamic elements (e.g., date/time). Freeze them with
cy.clock()or mock API responses. - Plugin not working in CI – Ensure the plugin is installed in the CI environment and that the snapshot directory is committed or properly generated before tests run.
- Tests pass locally but fail in CI – Likely due to differences in font rendering or screen resolution. Use a Docker image with the same OS and browser versions.
- Performance issues with many screenshots – Limit the number of screenshots per test and consider using a dedicated visual regression runner.
Frequently Asked Questions
- Can I use this with Cypress 9? – Yes, but you'll need to configure the plugin in
cypress/plugins/index.jsinstead of the config file. - Where are the baseline screenshots stored? – By default, they are stored in
cypress/snapshots/. You can change this in the plugin configuration. - How do I ignore a specific area of the screenshot? – The plugin doesn't support masking natively, but you can use CSS to hide dynamic elements before capturing, or use a third-party tool like Percy.
- Can I compare screenshots across different browsers? – Yes, but be aware that rendering can vary slightly. It's best to use a dedicated visual testing service for cross-browser comparisons.
- What happens if the baseline is outdated? – The test will fail, and you'll need to update the baseline manually with the update flag.
- Is it possible to set a tolerance for pixel differences? – Yes, configure the
thresholdoption in the plugin's config. For example,threshold: 0.01means a 1% difference is allowed.