import type pixelMatch from 'pixelmatch' import type { Options as SsimDiffOptions } from 'ssim.js' import type { SnapshotMeta } from '../auto_snapshots/snapshot_meta.ts' import type { NAME } from './constants.ts' export type Awaitable = T | PromiseLike export interface ImageSnapshotTimeoutOptions { /** * Timeout for taking the snapshot. * * Default: 30000 ms (30 seconds) */ timeout?: number | undefined } export interface ImageSnapshotKeyOptions { /** * Customize the `snapshotKey` of the snapshot. */ snapshotKey?: string | undefined } export type ComparisonMethod = 'pixel' | 'ssim' export type FailureThresholdOptions = { /** * Failure threshold should measure in `pixel` or `percent`. * * Default is `pixel`. */ failureThresholdType?: 'pixel' | 'percent' | undefined /** * Failure tolerance threshold. * * If `failureThresholdType` is `pixel`, the value is the number of pixels that are allowed to be different. * If `failureThresholdType` is `percent`, the value is the percentage of pixels that are allowed to be different. * * Default is `0`. * * @example * ```ts * // Allow 10 pixels to be different * failureThresholdType: 'pixel' * failureThreshold: 10 * * // Allow 10% of pixels to be different * failureThresholdType: 'percent' * failureThreshold: 10 * ``` */ failureThreshold?: number | undefined } export type SsimComparisonOptions = { comparisonMethod: M /** * Custom options passed to 'ssim' */ diffOptions?: Partial | undefined } export type { SsimDiffOptions } export type PixelComparisonOptions = { comparisonMethod?: M | undefined /** * Custom options passed to 'pixelmatch' */ diffOptions?: PixelDiffOptions | undefined } export type PixelDiffOptions = Parameters[5] export type ImageSnapshotCompareOptions = (M extends 'ssim' ? SsimComparisonOptions : PixelComparisonOptions) & FailureThresholdOptions export type ImageSnapshotSubjectOptions = { /** * Specify the query of the subject element. * * If the query matches multiple elements, the first visible one is used. * If the test does not have an element with the specified query, * the `body` element will be used. * If the query only matches hidden elements, the snapshot fails right away. */ subject?: string | undefined } export type ToMatchImageSnapshotOptions = ImageSnapshotTimeoutOptions & ImageSnapshotKeyOptions & ImageSnapshotCompareOptions & { /** * Expect the matcher to fail. * If it passes, it will throw an error with details. */ expectToFail?: boolean | undefined /** * Create a baseline image when none exists on disk. * * If `true`, the captured image is written to the baseline path and the assertion succeeds, * without Vitest `-u` / `updateSnapshot: 'all'`. * * @default false */ createMissingBaseline?: boolean | undefined } export interface PageImageSnapshotOptions { fullPage?: boolean | undefined } export type ToMatchPageImageSnapshotOptions = ToMatchImageSnapshotOptions & PageImageSnapshotOptions export type SnapshotTestMeta = { meta: { [NAME]?: SnapshotMeta } } export type ImageSnapshotComparisonInfo = { /** * Path to the project root. */ projectRoot: string /** * Path to the baseline image relative to the project root. */ baselinePath: string /** * Path to the result image relative to the project root. */ resultPath: string /** * Path to the diff image relative to the project root. */ diffPath: string /** * Base64 encoded baseline image. * Undefined if the baseline image does not exist. */ baseline?: string | undefined /** * Base64 encoded result image. */ result: string /** * Base64 encoded diff image. */ diff?: string | undefined } & ImageSnapshotTimeoutOptions & FailureThresholdOptions & (SsimComparisonOptions | PixelComparisonOptions) export type MatchImageSnapshotOptions = ImageSnapshotTimeoutOptions & ImageSnapshotCompareOptions & ImageSnapshotKeyOptions & { /** * When `true`, write a missing baseline on first run (see {@link ToMatchImageSnapshotOptions} `createMissingBaseline`). */ createMissingBaseline?: boolean | undefined } export type SetupVisOptions | unknown = unknown> = { auto?: | boolean | ( | unknown = unknown>( options: SnapshotMeta & M & GM, ) => Promise | Promise | boolean | void) | Record< string, | boolean | ( | unknown = unknown>( options: SnapshotMeta & M & GM, ) => Promise | Promise | boolean | void) > /** * Default {@link ToMatchImageSnapshotOptions#createMissingBaseline} for auto snapshots. * * @default false */ createMissingBaseline?: boolean | undefined }