Smart Systems for Home

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

Loki Storybook Visual Regression Setup

· 5 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

This guide walks you through setting up Loki for visual regression testing with Storybook. By the end, you'll have a command that screenshots every story, compares it against a baseline, and fails the build when UI changes appear — catching unintended style or layout regressions before they reach production. You'll need an existing Storybook project (v6 or later works best), Node.js installed, and a basic familiarity with running npm scripts.

What You'll Need

  • A Storybook project (v6.4+ recommended)
  • Node.js 14 or newer
  • npm or yarn
  • Chrome or Chromium installed (Loki uses Puppeteer by default)
  • Git (for the baseline commit process)

Step-by-Step

  1. Install Loki and its dependencies. Run npm install --save-dev loki @chromatic-com/storybook (or yarn add --dev loki @chromatic-com/storybook). The second package is a lightweight Storybook addon that Loki needs to extract story metadata.
  2. Add Loki's configuration file. Create a loki.config.js file in your project root. Start with this minimal setup:
    module.exports = {
      configDir: '.storybook',
      targets: {
        'chrome': {
          preset: 'loki-chrome'
        }
      }
    };

    Adjust configDir if your Storybook config lives elsewhere.
  3. Add npm scripts. In your package.json, add:
    "scripts": {
      "loki:update": "loki update",
      "loki:test": "loki test",
      "loki:approve": "loki approve"
    }

    This gives you three commands: update baselines, run tests, and approve new baselines.
  4. Run the initial baseline update. Execute npm run loki:update. Loki will start a static Storybook server, take screenshots of all stories, and save them in a .loki folder. This first run creates the reference images.
  5. Review the generated baselines. Check the .loki directory — you'll see a baseline folder with PNG files named by story ID. Open a few to confirm they look correct (no overlays, proper viewport). If something looks off, fix the component or adjust Loki's viewport settings in the config.
  6. Commit the baselines. Add the .loki folder to your repository. This is crucial — your CI and teammates need these reference images to compare against. Also commit loki.config.js.
  7. Run the test command. After making changes to your components, run npm run loki:test. Loki will take fresh screenshots and compare them pixel-by-pixel with the baselines. It outputs a summary of passed/failed stories.
  8. Handle failures. If a test fails, Loki generates a diff image in .loki/diff and an actual screenshot in .loki/current. Open these to see exactly what changed. If the change is intentional, run npm run loki:approve to promote the current screenshots to the new baseline.
  9. Integrate into CI. Add npm run loki:test to your CI pipeline (e.g., GitHub Actions, CircleCI). Make sure the CI environment has Chrome installed, or use a Docker image that includes it. Loki also supports a --ci flag that outputs JUnit-style reports.

Common Mistakes

  • Forgetting to commit baselines. If you skip step 6, every CI run will fail because there's no reference to compare against.
  • Running tests without starting Storybook. Loki starts its own server, so don't manually run Storybook first — it will conflict on the port.
  • Ignoring viewport differences. Loki uses a default viewport of 1024x768. If your components are responsive, configure multiple viewports in loki.config.js using the viewport option.
  • Using the wrong preset. The default preset is loki-chrome which uses Puppeteer. If you're in a Docker environment, you might need loki-chrome-docker.

Tips

  • Use a dedicated threshold. Set diffThreshold in the config (e.g., 0.001) to allow for minor anti-aliasing differences. Too low (0) causes false positives; too high misses real changes.
  • Group stories by viewport. Use Loki's viewports option to define multiple sizes, and use Storybook's parameters.viewport to assign specific viewports to stories.
  • Run Loki on a clean checkout. Before approving new baselines, ensure you're on a clean branch with no uncommitted changes — otherwise you might approve screenshots that include temporary modifications.
  • Use --filter for targeted runs. For example, loki test --filter=Button tests only stories containing "Button" in the ID, speeding up local development.
  • Keep baselines out of .gitignore. It's tempting to ignore the .loki folder, but that defeats the purpose. Add it to your repo, but consider using Git LFS if the images become large.

Troubleshooting

  • Loki fails with "No stories found". Check that your Storybook config is correct and the configDir points to the right folder. Also, ensure you have at least one story file.
  • Chrome not found error. Loki uses Puppeteer which downloads its own Chromium. If it fails, run npx puppeteer browsers install chrome or install Chrome manually and set the chromeFlags in the config.
  • Port already in use. Loki defaults to port 9009. If that's occupied, set port in the config to an available port.
  • Diff images show huge differences for no reason. This often happens when fonts load differently (e.g., web fonts not ready). Add delay in the config (e.g., 500ms) to give time for fonts to load, or use Loki's waitFor option.
  • Tests pass locally but fail in CI. Likely a rendering environment difference (OS, font rendering). Use Docker for CI to match your local environment, or increase the diffThreshold slightly.

Frequently Asked Questions

Does Loki work with Storybook 7?

Yes, Loki supports Storybook 7, but you need to use the @chromatic-com/storybook addon (version 1.0+). The setup steps are the same.

Can I use Loki with other browsers?

Loki supports Firefox and WebKit through presets like loki-firefox and loki-webkit. Note that cross-browser testing is slower and may require additional dependencies.

How do I test specific stories only?

Use the --filter flag with a regex pattern, e.g., loki test --filter=Button. You can also use --filter with loki update to update only those baselines.

What's the difference between loki test and loki approve?

loki test compares current screenshots to baselines and reports differences. loki approve takes the current screenshots and makes them the new baselines — use it when you've reviewed the changes and they're intentional.

Can I integrate Loki with Jest or Playwright?

Loki is standalone, but you can run it as part of a script before or after your Jest tests. For Playwright, you'd typically use Playwright's own screenshot comparison instead, as Loki is Storybook-focused.

How do I handle animations in stories?

Loki waits for the page to be stable, but CSS animations can cause flakiness. Use Storybook's parameters to disable animations in stories, or set chromeFlags: ['--disable-animations'] in the config.