Skip to content

feat: expose takeSnapshot() as public API#300

Merged
AriPerkkio merged 9 commits intomainfrom
ari/feat-expose-takesnapshot
Apr 13, 2026
Merged

feat: expose takeSnapshot() as public API#300
AriPerkkio merged 9 commits intomainfrom
ari/feat-expose-takesnapshot

Conversation

@AriPerkkio
Copy link
Copy Markdown
Member

@AriPerkkio AriPerkkio commented Apr 8, 2026

Issue: Related to #295

Stacks on #296.

What Changed

Finalize takeSnapshot() API and expose it as part of public API

takeSnapshot()

Users can generate snapshots in the middle of test run using takeSnapshot() API. This allows users to take multiple snapshots in a single test case. It’s also very useful when global disableAutoSnapshot is used.

import { takeSnapshot } from "@chromatic-com/vitest";

test("accordion open", async () => {
	const screen = await render(<Accordion />);
	await takeSnapshot();

	await screen.getByRole('button', { name: 'Open' }).click();
	await takeSnapshot("Accordion open");
});

If user calls takeSnapshot inside a test case that was not registered by the plugin, we will throw an informative error message:

export default defineConfig({
  plugins: [chromaticPlugin({ tags: ["visual-regression"] })],
})
import { takeSnapshot } from "@chromatic-com/vitest";

test("accordion", { tags: ["visual-regression"] }, async () => {
	await render(<Accordion />);

  // Works fine as test matches tags ✅
	await takeSnapshot();
});

test("accordion open", async () => {
	const screen = await render(<Accordion />);

	await screen.getByRole('button', { name: 'Open' }).click();

  // Throws error as test wasn't registered by the plugin due to missing tag ❌
  await takeSnapshot("Accordion open");
});
AssertionError: takeSnapshot() cannot be called in a test that is not registered for Chromatic plugin

  test/accordion.test.tsx:6:9
      4|   await screen.getByRole('button', { name: 'Open' }).click();
      5|
      6|   await takeSnapshot("Accordion open");
       |         ^
      7| });	

How to test

$ cd <project-root>
$ yarn test:unit
$ yarn test:vitest
$ yarn archive-storybook:vitest

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 8, 2026

⚠️ No Changeset found

Latest commit: a558e76

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 79.16667% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.43%. Comparing base (35ded1d) to head (a558e76).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/vitest/src/browser/public/takeSnapshot.ts 86.36% 3 Missing ⚠️
packages/vitest/src/node/commands.ts 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #300      +/-   ##
==========================================
+ Coverage   93.02%   94.43%   +1.40%     
==========================================
  Files          19       19              
  Lines         502      521      +19     
  Branches       86       92       +6     
==========================================
+ Hits          467      492      +25     
+ Misses         33       27       -6     
  Partials        2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Base automatically changed from ari/feat-minimal-vitest-integration to main April 13, 2026 05:13
@AriPerkkio AriPerkkio merged commit 9fa172d into main Apr 13, 2026
9 of 13 checks passed
@AriPerkkio AriPerkkio deleted the ari/feat-expose-takesnapshot branch April 13, 2026 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants