Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Install Reg Suit in React: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Reg Suit is a visual regression testing tool that helps you catch unintended UI changes by comparing screenshots of your React components before and after code changes. This guide walks you through installing and configuring Reg Suit in a React project, from setup to running your first test. You'll need a React project (created with Create React App, Vite, or similar) and Node.js installed on your machine.

What You'll Need

  • A React project (any modern setup works)
  • Node.js (version 14 or later recommended)
  • A package manager: npm (comes with Node) or yarn
  • Basic familiarity with the command line
  • A browser (Chrome or Firefox) for running tests

Step-by-Step

  1. Install Reg Suit and its dependencies. Open your terminal in the project root and run: npm install --save-dev reg-suit reg-cli (or yarn add --dev reg-suit reg-cli). Reg Suit orchestrates the testing, while reg-cli handles the actual screenshot comparison.
  2. Initialize Reg Suit. Run npx reg-suit init. This command will ask you a few questions about your setup. For a basic React project, accept the defaults unless you have specific needs. It will create a regconfig.json file in your project root.
  3. Review the generated configuration. Open regconfig.json. It should look something like this (values may vary):
    {
      "core": {
        "actualDir": "__screenshots__",
        "thresholdRate": 0.01,
        "addIgnore": true
      },
      "plugins": {
        "reg-notify-github": {
          "enabled": false
        }
      }
    }
    The actualDir is where your new screenshots will be stored. thresholdRate is the maximum allowed difference (1% by default). You'll adjust these later if needed.
  4. Create a script for taking screenshots. Reg Suit doesn't take screenshots itself; it compares them. You need a tool to capture your components. A common choice is puppeteer. Install it: npm install --save-dev puppeteer. Then create a script (e.g., scripts/screenshot.js) that launches a headless browser, visits your app, and takes screenshots of specific elements. Here's a minimal example:
    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('http://localhost:3000');
      // Replace with your component selector
      await page.screenshot({ path: '__screenshots__/home.png' });
      await browser.close();
    })();
    Ensure the output directory (__screenshots__) exists or create it in the script.
  5. Set up the expected screenshots (baseline). Run your screenshot script once to generate the initial set of images. These will be your baseline. Then run npx reg-suit run. Reg Suit will copy these into the expected directory (by default, it's stored in .reg).
  6. Modify your component and test again. Make a change to your React component (e.g., change a color or padding). Run your screenshot script again to capture new screenshots. Then run npx reg-suit run again. Reg Suit will compare the new screenshots against the baseline and report any differences.
  7. Review the report. After running, Reg Suit generates an HTML report in .reg directory. Open index.html in a browser to see a visual diff. It will highlight changed areas and show pass/fail status.
  8. Update the baseline when changes are intentional. If the UI change is intentional and you want to accept it as the new baseline, run npx reg-suit run --update. This updates the expected screenshots.
  9. Integrate into your workflow (optional). Add the screenshot script and reg-suit run to your package.json scripts for easy execution. For example:
    "scripts": {
      "screenshot": "node scripts/screenshot.js",
      "reg": "npm run screenshot && reg-suit run"
    }
    Now you can run npm run reg to take screenshots and compare in one go.

Common Mistakes

  • Forgetting to start the dev server. Puppeteer needs your app to be running at the URL you specified. If you don't start npm start before running the screenshot script, it will fail with a timeout.
  • Not cleaning up the actual directory. If you have old screenshots in __screenshots__ that are no longer relevant, they'll be compared against the baseline and cause false failures. Delete the directory before taking new screenshots, or use a script that clears it.
  • Ignoring the threshold rate. The default threshold is 1%, which is fine for most cases. However, if you have anti-aliased text or subtle gradients, you might get false positives. Adjust thresholdRate in the config to a higher value (e.g., 0.02) if needed.
  • Using a different browser version. Screenshots can vary slightly between browsers and even browser versions. Make sure you're using the same browser for baseline and test runs.

Tips

  • Use a dedicated test URL. Instead of testing your live dev server, consider running a separate build with a fixed port (e.g., npm run build && npx serve -s build -l 3000) to ensure consistency.
  • Name your screenshots meaningfully. Use component names or page names (e.g., button-primary.png) so you can easily identify what failed.
  • Integrate with CI. Reg Suit works well in CI pipelines. You can run it on every pull request and fail the build if there are unexpected changes. Use the GitHub plugin or just check the exit code.
  • Keep the baseline in version control. The .reg directory (or the expected directory) should be committed to your repository so that all team members have the same baseline.

Troubleshooting

  • Error: "reg-suit: command not found" – This usually means the installation didn't complete. Run npm install --save-dev reg-suit reg-cli again and check for errors. If you're using npx, ensure you're in the project root.
  • Puppeteer fails to launch – Make sure you have the necessary system dependencies (e.g., on Linux, you might need libnss3 and libatk). Try running npx puppeteer browsers install chrome to force-download the browser. If you're in a CI environment, you may need to set --no-sandbox in the launch options.
  • Reg Suit says "No expected screenshots found" – This happens when you haven't run the initial baseline. Run npx reg-suit run once without any existing expected screenshots; it will create them from the actual directory. Make sure your screenshot script has generated images in actualDir.
  • The report shows 100% difference for every image – This often means the screenshots are blank or the browser didn't load the page. Check the screenshot files in __screenshots__ – if they're empty or white, your script has a problem (e.g., wrong URL or selector). Also, ensure the page has finished loading; add a wait for network idle in Puppeteer: await page.goto(url, { waitUntil: 'networkidle0' });
  • Reg Suit hangs or takes too long – If you have many screenshots, it can be slow. Reduce the number of screenshots or increase the timeout in your config. Also, make sure your dev server is responsive.

Frequently Asked Questions

Do I need a separate server for Reg Suit?

No, Reg Suit doesn't require a server. It just compares image files. You only need a server to serve your React app so that Puppeteer can take screenshots.

Can I use Reg Suit with TypeScript?

Yes, Reg Suit is language-agnostic. It only cares about the screenshot images. You can use TypeScript for your React app and your screenshot scripts.

How do I ignore a specific area of the screen?

Reg Suit doesn't support ignoring areas directly in the config. However, you can modify your screenshot script to crop or mask areas before saving, or you can use CSS to hide elements during testing (e.g., add a class that hides dynamic content).

What if I have dynamic content like dates or random data?

Dynamic content will cause false positives. To handle this, you can either mock that data in your test environment (e.g., using a fixed date), or you can exclude those elements from the screenshot by taking screenshots of specific components rather than the whole page.

Can I use Reg Suit with Storybook?

Yes, Reg Suit works well with Storybook. You can use Storybook's visual testing addon or write a script that visits each story URL and takes a screenshot. Many teams use this combination for component-level regression testing.

Is Reg Suit free to use?

Yes, Reg Suit is open-source and free to use. There are no paid tiers or limitations on the core features.