import { assertSnapshotKeyWithoutDash } from '../shared/asserts.ts' import { NAME } from '../shared/constants.ts' import type { ComparisonMethod } from '../shared/types.ts' import { ctx } from './_ctx.ts' import type { SnapshotMeta } from './snapshot_meta.ts' type Suite = { meta: Record; suite?: Suite | undefined } export type MetaTask = | { file?: { meta: Record } | undefined suite?: Suite | undefined meta: Record } | undefined /** * Set the snapshot options for auto snapshot. * * You can use it in beforeAll, beforeEach, or individual test cases. * * @param meta - The snapshot options to set. * You can pass a boolean to enable/disable the snapshot, or an object with the snapshot options. * When passing an object, the `enable` property will be set to `true` by default unless you explicitly set it to `false`. * @param meta.enable - Whether to enable the snapshot. * @param meta.snapshotKey - The key to use for the snapshot. * @param meta.comparisonMethod - The comparison method to use for the snapshot. * @param meta.diffOptions - The diff options to use for the snapshot. * @param meta.failureThreshold - The failure threshold to use for the snapshot. * @param meta.failureThresholdType - The failure threshold type to use for the snapshot. * @param meta.timeout - The timeout to use for the snapshot. * * @example * * ```ts * beforeAll(() => setAutoSnapshotOptions(...)) * beforeEach(() => setAutoSnapshotOptions(...)) * * it('...', () => { * setAutoSnapshotOptions(...) * }) * ``` */ export function setAutoSnapshotOptions(meta: boolean | SnapshotMeta): void { if (typeof meta === 'object') { assertSnapshotKeyWithoutDash(meta.snapshotKey) } const task = getTask() if (!task) return task.meta[NAME] = { ...task.meta[NAME], ...parseMeta(meta), } } function getTask(): MetaTask | undefined { return ctx.getCurrentTest() ?? (ctx.getCurrentSuite()?.tasks?.[0] as any)?.file } function parseMeta(meta: boolean | SnapshotMeta): SnapshotMeta { return typeof meta === 'boolean' ? ({ enable: meta } as any) : { enable: true, ...meta } }