Loki Storybook Visual Regression Setup
In this article
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
- Install Loki and its dependencies. Run
npm install --save-dev loki @chromatic-com/storybook(oryarn add --dev loki @chromatic-com/storybook). The second package is a lightweight Storybook addon that Loki needs to extract story metadata. - Add Loki's configuration file. Create a
loki.config.jsfile in your project root. Start with this minimal setup:module.exports = {
configDir: '.storybook',
targets: {
'chrome': {
preset: 'loki-chrome'
}
}
};
AdjustconfigDirif your Storybook config lives elsewhere. - 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. - 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.lokifolder. This first run creates the reference images. - Review the generated baselines. Check the
.lokidirectory — you'll see abaselinefolder 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. - Commit the baselines. Add the
.lokifolder to your repository. This is crucial — your CI and teammates need these reference images to compare against. Also commitloki.config.js. - 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. - Handle failures. If a test fails, Loki generates a diff image in
.loki/diffand an actual screenshot in.loki/current. Open these to see exactly what changed. If the change is intentional, runnpm run loki:approveto promote the current screenshots to the new baseline. - Integrate into CI. Add
npm run loki:testto 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--ciflag 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.jsusing theviewportoption. - Using the wrong preset. The default preset is
loki-chromewhich uses Puppeteer. If you're in a Docker environment, you might needloki-chrome-docker.
Tips
- Use a dedicated threshold. Set
diffThresholdin 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
viewportsoption to define multiple sizes, and use Storybook'sparameters.viewportto 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
--filterfor targeted runs. For example,loki test --filter=Buttontests only stories containing "Button" in the ID, speeding up local development. - Keep baselines out of .gitignore. It's tempting to ignore the
.lokifolder, 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
configDirpoints 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 chromeor install Chrome manually and set thechromeFlagsin the config. - Port already in use. Loki defaults to port 9009. If that's occupied, set
portin 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
delayin the config (e.g., 500ms) to give time for fonts to load, or use Loki'swaitForoption. - 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
diffThresholdslightly.
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.