import { ScreenshotMetadata } from "@snapvisor/util"; import { ScreenshotMetadata as ScreenshotMetadata$1 } from "@snapvisor/util/browser"; import "@snapvisor/core"; import { ArgosAttachment } from "@snapvisor/playwright"; import { StabilizationPluginOptions, ViewportOption } from "@snapvisor/browser"; //#region src/storybook.d.ts /** * Whether a test renders a Storybook story, and which one. Gathered on the test * side and sent to the Node command, which reports the screenshot as a * Storybook one. */ type StorybookContext = { /** * How the story runs: * - `addon-vitest`: `@storybook/addon-vitest` turned the story into a test; * - `portable-stories`: the test rendered a composed story * (`composeStories()`), with `Story.run()` or as a component. */ source: "addon-vitest" | "portable-stories"; /** * The story, or `null` when Storybook gave it a placeholder id, shared by * unrelated stories (see {@link checkIsPlaceholderId}). */ story: NonNullable | null; }; //#endregion //#region src/metadata.d.ts /** The `test` slice of {@link ScreenshotMetadata}. */ type TestMetadata = ScreenshotMetadata["test"]; //#endregion //#region src/options.d.ts /** * Options passed when calling `argosScreenshot` from a browser test. * * These options cross the Vitest browser/node RPC boundary, so they must be * JSON-serializable. Non-serializable options (`beforeScreenshot`, * `afterScreenshot`, a `Locator`/`ElementHandle` `element`, …) can only be set * on the plugin via {@link ArgosVitestPluginOptions}. */ interface VitestScreenshotOptions { /** * String selector of the element to take a screenshot of. * A `Locator`/`ElementHandle` cannot be used here because it can't be * serialized across the browser/node boundary — set it on the plugin instead. */ element?: string; /** * Viewports to take screenshots of. * Implemented by resizing the Vitest iframe (Playwright's native `viewports` * option does not work on a frame). */ viewports?: ViewportOption[]; /** * Capture the full page instead of fitting the screenshot to the content. * - `false` (default): the iframe grows to fit the content in both * dimensions, so nothing is clipped. * - `true`: keep the viewport width and grow the height (Playwright-style * full page). * @default false */ fullPage?: boolean; /** * Custom CSS evaluated during the screenshot process. */ argosCSS?: string; /** * Selectors of the elements to hide from the screenshot. * Hidden elements keep their space in the layout, like with the * `data-visual-test="transparent"` helper. * Selectors that match nothing are ignored. */ hide?: string[]; /** * Selectors of the elements to remove from the screenshot. * Removed elements no longer take space in the layout, like with the * `data-visual-test="removed"` helper. * Selectors that match nothing are ignored. */ remove?: string[]; /** * Selectors of the elements to cover with a box when the screenshot is * taken, like with the `data-visual-test="blackout"` helper. * Selectors that match nothing are ignored. */ mask?: string[]; /** * Color of the box covering the masked elements, in CSS color format. * @default "#FF00FF" */ maskColor?: string; /** * Sensitivity threshold between 0 and 1. * The higher the threshold, the less sensitive the diff will be. * @default 0.5 */ threshold?: number; /** * Name, or list of names, to compare this screenshot against instead of its * own name. A list is tried in order and the first name found in the baseline * build wins, which lets a brand new screenshot fall back to an existing one * rather than showing up as added. * * The screenshot's own name is not implicitly included: list it first to keep * comparing against itself when it exists. * * @example * // Compare "home-variant-b" against itself, or against "home" if it is new. * argosScreenshot("home-variant-b", { * baseName: ["home-variant-b", "home"], * }) */ baseName?: string | string[]; /** * Tag or array of tags to attach to the screenshot. */ tag?: string | string[]; /** * Capture an ARIA snapshot along with the screenshot. * @default false */ ariaSnapshot?: boolean; /** * Disable hover effects by moving the mouse to the top-left corner. * @default true */ disableHover?: boolean; /** * Wait for the UI to stabilize before taking the screenshot. * Set to `false` to disable stabilization or pass an object to customize it. * @default true */ stabilize?: boolean | StabilizationPluginOptions; } /** * Options passed when calling `argosSnapshot`. * * `argosSnapshot` serializes any value to a file that Snapvisor picks up and diffs, * mimicking {@link https://vitest.dev/guide/snapshot Vitest snapshots}. Unlike * `argosScreenshot`, it does not need a browser and works both in Vitest browser * tests and in plain Node tests. * * These options must be JSON-serializable so they can cross the Vitest * browser/node RPC boundary — the only exception is `serialize`, which is * applied on the test side *before* the value is sent to Node. */ interface VitestSnapshotOptions { /** * Unique name of the snapshot. * * When omitted, Snapvisor generates one automatically from the current test, * mimicking {@link https://vitest.dev/guide/snapshot Vitest's snapshot naming}. * The generated name includes the test file path so names stay unique across * files (Snapvisor names are global across the build, unlike Vitest's per-file * `.snap`). */ name?: string; /** * Folder where the snapshot is written. * * In Node tests this defaults to `"./snapshots"`. In browser tests it * defaults to the plugin `root` and can be overridden per call. * @default "./snapshots" */ root?: string; /** * Extension of the snapshot file. It also determines how Snapvisor renders and * diffs the snapshot (e.g. `.txt`, `.json`, `.yml`, `.html`, `.md`). * @default ".txt" */ extension?: string; /** * Tag or array of tags to attach to the snapshot. */ tag?: string | string[]; /** * Custom serializer used when `content` is not already a string. * Defaults to `@vitest/pretty-format` (the serializer Vitest itself uses). */ serialize?: (content: unknown) => string; } /** * Subset of {@link VitestSnapshotOptions} that can cross the Vitest * browser/node RPC boundary. Excludes `serialize` (applied before the value is * sent to Node) and `name` (resolved on the test side and passed to the Node * primitives as a separate argument). */ type SerializableSnapshotOptions = Omit; //#endregion //#region src/index.d.ts declare module "vitest/browser" { interface BrowserCommands { argosScreenshot: (name: string, options?: VitestScreenshotOptions, test?: TestMetadata, captureIndex?: number | null, storybook?: StorybookContext | null) => Promise; argosSnapshot: (name: string, content: string, options?: SerializableSnapshotOptions, test?: TestMetadata, captureIndex?: number | null, storybook?: StorybookContext | null) => Promise; } } /** * Take an Snapvisor screenshot in a Vitest browser test. * * Requires the {@link https://www.npmjs.com/package/@snapvisor/vitest Snapvisor Vitest plugin} * to be registered in your Vitest config. * * The `name` is optional: when omitted, Snapvisor generates one automatically from * the current test, mimicking {@link https://vitest.dev/guide/snapshot Vitest's * snapshot naming}. The name includes the test file path (Snapvisor names are global * across the build, unlike Vitest's per-file `.snap`), so names stay unique * across files. * * @example * ```ts * import { render } from "vitest-browser-react"; * import { argosScreenshot } from "@snapvisor/vitest"; * * test("Button", async () => { * render(); * await argosScreenshot("button"); // explicit name * await argosScreenshot(); // -> "src/Button.test.tsx > Button 1" * }); * ``` * * @param name - Unique name of the screenshot. Omit to generate one from the * current test. * @param options - Serializable screenshot options. * @returns The attachments captured, or an empty array outside of Vitest. */ export declare function argosScreenshot(name: string, options?: VitestScreenshotOptions): Promise; export declare function argosScreenshot(options?: VitestScreenshotOptions): Promise; /** * Take an Snapvisor snapshot of any serializable value, mimicking * {@link https://vitest.dev/guide/snapshot Vitest snapshots}. * * Unlike {@link argosScreenshot}, this does not need a browser: it serializes * the value (strings verbatim, everything else via `@vitest/pretty-format`) to a * file that Snapvisor picks up and diffs across builds. It works both in Vitest * browser tests and in plain Node tests. * * The name is optional: pass it via `options.name`, or omit it to have Snapvisor * generate one automatically from the current test, mimicking * {@link https://vitest.dev/guide/snapshot Vitest's snapshot naming}. The name * includes the test file path (Snapvisor names are global across the build, unlike * Vitest's per-file `.snap`), so names stay unique across files. * * @example * ```ts * import { argosSnapshot } from "@snapvisor/vitest"; * * test("API response", async () => { * const data = await fetchUser(); * await argosSnapshot(data); // -> "src/user.test.ts > API response 1" * await argosSnapshot(data, { name: "user" }); // explicit name * }); * ``` * * @param content - The value to snapshot. Strings are written as-is; any other * value is serialized. * @param options - Snapshot options, including an optional `name`. * @returns The attachments written, or an empty array outside of Vitest. */ export declare function argosSnapshot(content: unknown, options?: VitestSnapshotOptions): Promise; /** * Check if we are running in a Vitest environment. */ export declare function checkIsVitestEnv(): Promise; /** * Check if we are running in Vitest browser mode. * Vitest sets this global in the browser runner. */ export declare function checkIsBrowserEnv(): boolean; /** * Snapvisor-named alias of {@link argosScreenshot}. Both names stay supported. */ export declare const snapvisorScreenshot: typeof argosScreenshot; /** * Snapvisor-named alias of {@link argosSnapshot}. Both names stay supported. */ export declare const snapvisorSnapshot: typeof argosSnapshot; //#endregion export type { VitestScreenshotOptions, VitestSnapshotOptions };