How to Debug Flaky Visual Regression Tests
In this article
Visual regression tests compare screenshots of your UI against a baseline to catch unintended styling changes. They become flaky when the same code produces different results run after run, often due to timing, rendering, or environment issues. This guide walks you through a systematic process to identify and fix the root causes, making your visual tests reliable and trustworthy.
What You'll Need
- Access to the test suite where the flaky tests are failing
- The ability to run tests locally and in CI (or at least view CI logs)
- Knowledge of your visual testing tool (e.g., Percy, Chromatic, Playwright, Cypress, BackstopJS)
- A version control system to revert changes if needed
Step-by-Step
- Reproduce the failure. Run the failing test multiple times locally and in CI. Note how often it fails (e.g., 1 in 5 runs) and whether it fails on specific pages or elements. If you can't reproduce locally, focus on CI logs and screenshots.
- Isolate the flaky element. Look at the diff image from the failed test. Identify exactly which part of the UI changed. Is it a font, an image, a layout shift, or an animation? If the diff is large, narrow it down by temporarily disabling parts of the page (e.g., hide a component) to see if the failure disappears.
- Check for dynamic content. Common culprits are dates, timestamps, random data, or third-party widgets (like ads or social feeds). If you see such content in the diff, mock or freeze it. For example, use a fixed system time in your test setup, or replace random data with a deterministic fixture.
- Inspect timing and waiting. Visual tests often fail because the page isn't fully loaded or animations haven't finished. Ensure your test waits for network requests to complete, images to load, and fonts to be ready. Use explicit waits for elements that appear after async operations, and disable CSS animations or transitions in the test environment.
- Review rendering differences. Fonts, anti-aliasing, and subpixel rendering can vary across operating systems and browsers. If you're running tests on different machines, standardize the environment. Use a fixed viewport size, disable hardware acceleration, and consider using a Docker container or a consistent CI runner.
- Check for race conditions. If the test interacts with the page (e.g., clicks a button), ensure that actions happen after the UI is ready. Use assertions to wait for expected states before taking a screenshot.
- Examine the baseline. Sometimes the baseline itself is outdated or was generated on a different environment. Update the baseline to match the current intended design, but only after confirming the change is intentional.
- Add tolerance where appropriate. Most visual testing tools allow you to set a pixel or percentage threshold for differences. Increase the threshold slightly for areas that are inherently variable (like gradients or shadows), but avoid masking real bugs.
- Run the test in isolation. If the test passes alone but fails when run with others, there may be test pollution. Check for shared state, overlapping selectors, or leftover data. Ensure each test cleans up after itself.
- Document and monitor. After fixing, run the test multiple times to confirm stability. Add a comment in the test explaining the fix. Monitor the test in CI over a few days to ensure it doesn't regress.
Common Mistakes
- Blindly increasing tolerance to make tests pass—this hides real issues.
- Ignoring environment differences between local and CI (different OS, fonts, or browser versions).
- Not disabling animations or using dynamic data in tests.
- Taking screenshots before the page is fully stable (e.g., before images load).
- Updating baselines without reviewing the diff—you might approve a bug.
Tips
- Use a dedicated test environment with a fixed date and timezone.
- Mock all network requests that return variable data (e.g., APIs, ads).
- Run visual tests on a single browser first, then expand to others to reduce variables.
- Use CSS to force consistent rendering: set `font-smoothing: antialiased` and `image-rendering: pixelated` for tests.
- Keep baselines in version control and review changes in pull requests.
- If using a cloud service, ensure your local environment matches the CI environment as closely as possible.
Troubleshooting
Test fails only in CI, not locally. Compare the browser version, OS, and installed fonts. Use the same Docker image or CI runner for local testing. Also check for differences in screen resolution or scaling.
Diff appears on a specific element but not consistently. That element might be rendering a timestamp, a random ID, or a web font that loads late. Add a wait for that element to be stable, or mock its content.
Test passes after a retry but fails on the first run. This suggests a timing issue. Increase the wait time before the screenshot, or use a more robust wait condition (e.g., wait for network idle).
Baseline updates don't fix the flakiness. The flakiness is likely caused by a variable in the environment, not the baseline. Re-examine the diff for dynamic content or rendering differences.
You see a diff in fonts or text rendering. Ensure the same font files are available in all environments. Use system fonts or embed fonts in the test environment. Disable subpixel rendering if possible.
Frequently Asked Questions
Why does my visual test pass locally but fail in CI?
Differences in operating system, browser version, screen size, or installed fonts can cause rendering variations. Standardize the CI environment and run locally in the same conditions (e.g., using Docker).
Should I increase the pixel threshold to make tests pass?
Only if the differences are truly insignificant (e.g., anti-aliasing). Increasing the threshold too much can allow real visual bugs to slip through. Use it sparingly and only for specific elements.
How do I handle animations in visual tests?
Disable CSS animations and transitions in your test environment. You can do this by injecting a style tag that sets `animation: none !important; transition: none !important;` before taking the screenshot.
What is the best way to mock dynamic data?
Use your test framework's mocking capabilities to intercept API requests and return fixed fixtures. For dates, use a library like `sinon` to fake the system time.
How often should I update baselines?
Update baselines only when the UI intentionally changes. Review the diff carefully before approving. For flaky tests, don't update baselines as a quick fix—find the root cause.
Can flaky visual tests be caused by the order of tests?
Yes, if tests share state or leave behind data. Ensure each test is independent, cleans up after itself, and doesn't rely on the previous test's actions.