Happo now works with Vitest Browser Mode. Add a plugin to your Vitest config, call happoScreenshot with an element from a test you already have, and Happo renders that element in every browser you pick and shows what your pull request changed, next to the screenshots from Storybook, Playwright, and Cypress you might already be sending. It's in version 7.1.0 of the happo npm package.
test('open', async () => {
render(<AccountMenu />);
await userEvent.click(screen.getByRole('button'));
await happoScreenshot(screen.getByRole('menu'), {
component: 'AccountMenu',
variant: 'open',
});
});
That's the whole idea. Here's why we built it, what to expect, how to set it up, and how to get the most out of it.
Why Vitest
A lot of the component testing we see has moved to Vitest. It's fast, it shares its config with Vite, and with Browser Mode your tests stop running in a simulated DOM like jsdom and start running in a real browser. That matters for tests that click, hover, focus, and type, because the browser is doing the layout, the events, and the CSS.
We've been moving in the same direction ourselves. happo.io's own component tests run in Vitest Browser Mode, and they send their screenshots to Happo through this same integration.
Those tests are really good at one thing: checking what a component does. It renders, it shows the right text, the click calls your handler. But a test like this one passes whether the card looks right or not:
expect(card).toBeVisible();
expect(price).toHaveTextContent('€412');
await userEvent.click(reserve);
expect(onReserve).toHaveBeenCalled();
The card rendered and the click called onReserve, so every assertion passed. Meanwhile, in Safari, the price wrapped under the button. Your test ran in a real browser, but it never looked at what that browser drew, and it never ran in any other browser at all. That's the gap Happo fills.
The nice part is that your tests have already done the hard work. They've rendered the component with realistic props, opened the menu, filled in the form, and triggered the validation error. Those are exactly the states you want screenshots of, and they're the states that are often the most tedious to reach any other way. One extra line hands that state to Happo.
What to expect
One plugin, one call. happoVitestPlugin goes in your Vitest config, and happoScreenshot takes the element your test already has: one from Testing Library's screen, or a locator from page in vitest/browser.
One browser in Vitest, every browser in Happo. Keep running your tests in a single Chromium instance. When you call happoScreenshot, Happo captures the element's HTML, CSS, and assets, and renders it again on its own machines in every target in your happo.config.ts, Safari included. You don't have to install or run those browsers yourself, and your screenshots don't change when your CI machine does.
Only passing tests count. Happo keeps a test's screenshots once that test has passed. A failing test's screenshots are dropped, and a test that Vitest retries keeps only the attempt that passed. If the run fails, there's no Happo report by default, so you're never asked to review screenshots of a broken build. (If you'd rather get a report of the passing tests anyway, set allowFailures: true.)
Out of the way locally. Happo only runs when you wrap vitest run with npx happo --, which you'll do in CI. In watch mode and in your editor, happoScreenshot does nothing, so your local feedback loop stays as fast as it is today.
Hover, focus, and pressed states. Hover a button or tab to it with userEvent, turn on applyPseudoClasses for your targets, and Happo shows that state in every browser.
Everything else you get from Happo. A status on every pull request linking to its diffs, sharded runs combined into one report, flake filtering and reporting, review in one place, and accessibility checks for every screenshot when you add an accessibility target. It all works the same way it does for our other integrations.
Setting it up
You'll need Vitest Browser Mode with the Playwright or WebdriverIO provider, and a Happo account. Tests that run in jsdom or happy-dom can stay as they are; you add screenshots to the ones that run in Browser Mode.
Install the happo package, version 7.1.0 or later. If you already have it, update it: older versions don't have happo/vitest.
npm install --save-dev happo
Add the plugin to your Vitest config. One browser instance is enough here, since Happo renders each screenshot again in every browser you pick:
import { playwright } from '@vitest/browser-playwright';
import { happoVitestPlugin } from 'happo/vitest';
import { defineConfig } from 'vitest/config';
export default defineConfig({
plugins: [happoVitestPlugin()],
test: {
browser: {
enabled: true,
provider: playwright(),
instances: [{ browser: 'chromium' }],
},
},
});
Take a screenshot in a test you already have:
import { render, screen } from '@testing-library/react';
import { happoScreenshot } from 'happo/vitest/browser';
import { test } from 'vitest';
import { userEvent } from 'vitest/browser';
import AccountMenu from './AccountMenu';
test('open', async () => {
render(<AccountMenu />);
await userEvent.click(screen.getByRole('button'));
await happoScreenshot(screen.getByRole('menu'), {
component: 'AccountMenu',
variant: 'open',
});
});
Pick your browsers in happo.config.ts:
import { defineConfig } from 'happo';
export default defineConfig({
integration: {
type: 'vitest',
},
targets: {
chrome: { type: 'chrome', viewport: '1024x768' },
safari: { type: 'safari', viewport: '1024x768' },
firefox: { type: 'firefox', viewport: '1024x768' },
},
});
And wrap vitest run in CI:
npx happo -- vitest run
Happo reads your API key and secret from the HAPPO_API_KEY and HAPPO_API_SECRET environment variables, and on GitHub Actions it can authenticate without them. If you shard your Vitest run across machines, the same --nonce and npx happo finalize steps you'd use for Playwright bring the shards together into one report. The Vitest guide has the details, along with every option.
Getting the most out of it
Adding happoScreenshot is easy, so the interesting question becomes where to add it. Here's how I'd think about it.
Screenshot the states your tests reach that nothing else does
If you use Storybook, your stories probably already cover a component's resting states: the default button, the disabled one, the one with a long label. Vitest tests shine somewhere else. They get a component into states that take interaction to reach: the open menu, the form after a failed submit, the combobox with a filtered list, the modal on its second step. Those are the screenshots that are hardest to get any other way, so start there.
You don't need a screenshot in every test. A test that checks a handler was called with the right arguments doesn't gain much from one. A test that ends with the component in a state a user will see does.
Assert first, then screenshot
Let your test wait for the state you want before you capture it. If the menu opens after a click, wait for it to be visible first:
await userEvent.click(screen.getByRole('button', { name: 'Menu' }));
await expect.element(page.getByText('Settings')).toBeVisible();
await happoScreenshot(container.firstElementChild, {
component: 'Dropdown',
variant: 'opened with a click',
});
This does double duty. The assertion makes the test meaningful on its own, and it means Happo captures the finished state rather than something halfway through rendering. Waiting for data, fonts, and images the same way keeps your screenshots stable from run to run.
Pass the element that shows the whole picture
Happo crops the screenshot to the element you pass. That's great for focus, but it means anything drawn outside the element is cut off: a focus ring made with outline or box-shadow, a dropdown that hangs below its trigger, a tooltip. When that matters, pass a wrapper with some room around it instead. The example above renders the dropdown inside a div with some padding and height, and screenshots that div, so the open menu is in the frame.
Name screenshots like you'd name stories
component and variant are how Happo matches a screenshot on your branch with the same one on your base branch, and how you'll find it in a report. Use the component's name for component, and describe the state in variant: open, with validation errors, hovered. Keep the names stable. Renaming one shows up as one screenshot deleted and another added, rather than as a diff.
Keep the data deterministic
A screenshot is only useful if it changes when your UI changes, and not otherwise. Today's date, random IDs, live network responses, and an avatar that loads from a third-party URL all make screenshots that differ between runs. Most of these are already pinned down in a good component test (mocked requests, fixed fixtures), but it's worth a second look before you add screenshots to a test. Happo holds animations still and filters out rendering noise below a threshold, and when something does slip through, you can report it as flake once and Happo ignores it from then on.
Clean up after hovering
The pointer stays where a test left it. A test that hovers something should move off it at the end with userEvent.unhover, or the next test's element may be captured hovered too.
Pick targets for what you want to learn
Since your tests only need one browser, the targets in happo.config.ts are where you decide what you're checking: the browsers your users have, a narrow viewport for your mobile layout, and an accessibility target to catch accessibility violations in the same run. If a few screenshots need something special, like a component that's only used on small screens, happoScreenshot takes a targets option to render that screenshot in just the targets that make sense for it.
Grow it the way you grow your tests
You don't have to add screenshots to your whole suite on day one. Start with the components where a visual bug would hurt the most, or the ones that have bitten you before, and add screenshots as you touch tests. Each one is a single line in a test that already exists, so the cost of adding coverage stays small, and there's no second suite to keep up. If you're watching your bill, our post on growing your Happo coverage without losing control of it applies here too.
Try it
The Vitest page has a quick tour, and the Vitest guide has everything you need to set it up. If you're new to Happo, you can start for free.
This is a new integration, and we'd love to hear how it goes for you. If something doesn't work the way you expect with your Vitest setup, or there's something you wish it did, send us a note at support@happo.io.
