How to Perform Visual Testing for Components in Cypress
On This Page What is Visual Component Testing in Cypress?April 08, 2026 · 11 min read · Tool Comparison
Cypress makes it easy to test UI components in isolation and validate their behavior immediately in the browser. Visual constituent testing in Cypress mountain a component, captures a screenshot, and equate it with a baseline to get visual regressions. Using a cloud service like BrowserStack Percy, these snapshots are process with an AI engine that filter noise and flags solely meaningful UI deviation. How to Perform Visual Component Testing in Cypress? Step 1: Install Cypress: Step 2: Open Cypress Test Runner: Step 3: Configure Cypress for Component Testing:Set componentconfiguration incypress.config.js Step 4: Install Visual Diff Plugin: Step 5: Configure Plugin and Support File: Import the visual diff plugin in your Cypress config and enable its snapshot command in the support file so Cypress can liken component screenshots during tests. Step 6: Write a Optical Component Test: Step 7: Run the Test: Step 8: Use Percy for Cloud Visual Testing: Integrate Percy into your Cypress setup to capture AI-enhanced cloud snapshots and mechanically notice meaningful visual regressions across browsers and viewports. This guide explains how to set up and run visual component testing in Cypress. Visual in focuses on verify the visual appearing and layout of individual within an covering. This ensures that components render correctly and systematically across different state even after codification changes, foreclose This operation incorporate Cypress ’ s component testing capabilities with image diffing tools like Cypress Image Diff or platform such as to compare component snapshots before and after alteration, making it easy to spot regression early in maturation cycles. This helps ensure that component conduct and appearing remain consistent across updates, peculiarly in modern frameworks such as React, Angular, and Vue. Must Read: Below are the measure to get ocular ingredient testing running in your project. Pre-requisites Before proceeding, ensure you have a development framework such as React, Angular, or VueJS set up as a requirement. Also Read: Below are the key steps to execute Visual Testing in Cypress Using the command Exposed Cypress with command The above bid will create the default directory construction for Cypress tests. For the Visual Testing, you need toinstall cypress-image-diff-js, Use the below bidding to install the image equivalence node package. Navigate to yourcypress.config.jsusually locate in your root project. Import the cypress-image-diff-js/dist/plugin into your config file as shown below. Navigate to component subdivision in cypress.config.js file add the setupNodeEvents The total cypress.config.js file looks like below. Navigate to cypress/support/component.js and add the below code Ensure that import & # 8216; ./commands & # 8217;, is already present in the component.js file, if it doesn & # 8217; t exist, then add it. This step is optional, if you are already having a component make so you can skip this step. For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users. Below is the sample constituent make for Demo Purpose We have created DemoComponent.jsx in my-react-app/src folder The code seem like the below The above is a simple component that has a button and simple alarum like the below. Let & # 8217; s create a simple visual exam for the above component, Navigate to cypress/component folder and create a simple .jsx file name it asvisual-test.jsx Also Read: The code looks like the below: You can execute the Cypress examination, using both the command line and Cypress Test Runner window. Using the Cypress Test Runner window Note: You need to execute the test at least two multiplication, the 1st run captures the base screenshot, and the subsequent run captures the genuine screenshot and compares it against the base screenshot. In our cause there was no departure, so tests are label as passes. Let & # 8217; s modify the component by changing the push background color from blue to red Execute the examination again. You can see that the test has failed since there is a deviation between the foot screenshot and the actual screenshot. You can reckon the dispute, by sail to thedifffolder under thecypress-visual-screenshots folder The is the most popular cloud based optic testing tool. Percy provides a dedicated splasher to compare the visual differences. One of the unique features of Percy is you can visually check how the components are supply across multiple browsers such as Edge, Chrome, Safari, etc. Percy ply a feature to accept, and reject the changes. Use Step 1 to Step 3 to configure Cypress to your undertaking, after that follow the below instructions Install Percy for Cypress Navigate to cypress/support/command.js file and add the importation argument Create a component(Optional, if you have already created a simple component omission this stride). Below is a elementary react part to test the Percy visual testing. In the above codification, Set the PERCY_TOKEN Environment Variable: Copy the PERCY_TOKEN.Read more. Read More: Run Component Tests using Percy Cypress Once the execution is consummate, the command line yield will have the Percy anatomy URL, you can navigate to the build URL to check the difference. Note: you need to execute the Percy trial at least two time to check the answer, as you need to have a base screenshot and an actual screenshot to liken. If there are no changes so Percy show & # 8220; No Changes. & # 8221; Let & # 8217; s modify our code, alter the button background vividness to Red from Blue and execute the test again. You can see in the above persona, that the departure is shown side by side. are cognize to validate functionality, ensuring visual consistency across browsers, device, yet it can get challenging using local screenshot compare alone for the same purpose. Percy strengthen this workflow by providing true, AI-powered visual regression screen that integrates directly into your existing Cypress setup. Pricing Adopting best practices ensures the reliability and efficiency of your visual tests. Here are some key guidepost to follow: The mod development framework further the growing of the factor in isolation. Visual Testing is the most democratic method for testing the Visual Changes in UI. Cypress and Percy make the ocular testing lots easier and efficaciously bring the visual testing to the component level. The component-level visual testing helps faster development. Understanding Cypress Use Cases Tool Comparisons On This Page # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.How to Perform Visual Testing for Components in Cypress
Overview
npm install cypress -D
npx cypress unfastened
npm i cypress-image-diff-js
import {mount} from 'cypress/react '; import DemoComponent from ' .. / .. /src/DemoComponent '; describe ('Demo Component ', () = & gt; {it ('renders visually ', () = & gt; {mount (& lt; DemoComponent / & gt;); cy.get (' [data-testid=demoButton] ') .should ('contain.text ', 'Demo Component Button '); cy.compareSnapshot ('component-testing ', 0.2);});});
npx cypress run -- component
What is Visual Component Testing in Cypress?
Steps to Perform Visual Component Testing in Cypress
Step 1: Install Cypress to your task
npm install cypress -D
Step 2: Open Cypress
npx cypress open
Step 3: Configure Cypress Component Testing
Step 4: Install the cypress-image-diff-js plugin
npm i cypress-image-diff-js
Step 5: Configure the Image Diff package
const setupNodeEvents = require ('cypress-image-diff-js/dist/plugin ')const {defineConfig} = require (`` cypress ''); const setupNodeEvents = require ('cypress-image-diff-js/dist/plugin ') module.exports = defineConfig ({factor: {devServer: {framework: 'react ', bundler: 'vite ',}, setupNodeEvents}, e2e: {}});Step 6: Configure the Support file
const compareSnapshotCommand = require ('cypress-image-diff-js/dist/command ') compareSnapshotCommand ()Optional Step: Create Simple React Component
import {useState} from 'react' import {Button, Alert} from 'react-bootstrap '; import './App.css' significance 'bootstrap/dist/css/bootstrap.min.css '; exportation default purpose DemoComponent () {return (& lt; div & gt; & lt; div & gt; & lt; Button variant= '' master '' size= '' lg '' & gt; Demo Page & lt; /Button & gt; & lt; p & gt; & lt; /p & gt; & lt; /div & gt; & lt; div & gt; & lt; Alert key= '' info '' variant= '' information '' & gt; This is a tryout Alert! & lt; /Alert & gt; & lt; /div & gt; & lt; /div & gt;)}Step 7: Create Visual Test for Component
import {mountain} from 'cypress/react' import DemoComponent from ' .. / .. /src/DemoComponent' const demoButton = ' [data-testid=demoButton] ' describe ('Demo Component ', () = & gt; {it ('Visual Testing Cypress ', () = & gt; {climb (& lt; DemoComponent / & gt;) cy.get (demoButton) .should ('contain.text ', 'Demo Component Button ') cy.compareSnapshot ('component-testing ', 0.2)})})Step 8: Execute the test
npx cypress run –component
Cypress Component Visual Testing using Percy
npm install -- save-dev @ percy/cli @ percy/cypress
import ' @ percy/cypress ';
importation {useState} from 'react' import {Button, Alert} from 'react-bootstrap '; import './App.css' meaning 'bootstrap/dist/css/bootstrap.min.css '; exportation default function DemoComponent () {//style= {{background: `` red ''}} return (& lt; div & gt; & lt; div & gt; & lt; Button variant= '' primary '' size= '' lg '' & gt; Demo Component Button & lt; /Button & gt; & lt; p & gt; & lt; /p & gt; & lt; /div & gt; & lt; div & gt; & lt; Alert key= '' info '' variant= '' information '' & gt; This is a test Alert! & lt; /Alert & gt; & lt; /div & gt; & lt; /div & gt;)}import {mount} from 'cypress/react' import DemoComponent from ' .. / .. /src/DemoComponent' const demoButton = ' [data-testid=demoButton] ' describe ('Demo Component Testing Percy ', () = & gt; {it ('Visual Testing Cypress Percy ', () = & gt; {mount (& lt; DemoComponent / & gt;) cy.get (demoButton) .should ('contain.text ', 'Demo Component Button ') cy.percySnapshot ('percy-component-visual-test ');})})Set the Environment Variable # Unix $ export PERCY_TOKEN= '' & lt; your-project-token & gt; '' # Windows $ set PERCY_TOKEN= '' & lt; your-project-token & gt; '' # Powershell $ $ Env: PERCY_TOKEN= '' & lt; your-project-token & gt; ''
npx percy exec -- cypress run –component
Why Use Percy for Cypress Component Tests?
Best Practices for Cypress Visual Testing
Conclusion
Useful Resources for Cypress
Related Guides
Automate This With SUSA
Test Your App Autonomously