// `storybook-addon-vis` provides code that can be used in test files. import './shared/global_matcher_augment.ts' import { definePreviewAddon } from 'storybook/internal/csf' import { expect } from 'storybook/test' import type { SetupVisOptions } from 'vitest-plugin-vis' import { autoSnapshotMatcher, setAutoSnapshotOptions, toSuiteDefaults } from 'vitest-plugin-vis/client-api' import { toMatchImageSnapshot } from './client/expect/to_match_image_snapshot.ts' import { isVitestBrowser } from './client/is_vitest_browser.ts' import { cdp, commands, page, whenVitestProxyReady } from './client/vitest_proxy.ts' import { visAnnotations } from './preview/vis_annotation.ts' // Register at module load time so it's available in Storybook dev mode. // (visAnnotations.beforeEach is only wired via vitest's setProjectAnnotations, not in dev mode) expect.extend({ toMatchImageSnapshot }) export * from 'vitest-plugin-vis' export * from './client/has_image_snapshot.ts' export * from './client/storybook/param.ts' export * from './client/storybook/tags.ts' export { cdp, page, visAnnotations } export default (options: SetupVisOptions<{ tags: string[] }> = { auto: false }) => { let matcher: ReturnType let matcherFn: () => Promise return definePreviewAddon({ tags: options.auto === true ? ['snapshot'] : [], async beforeAll() { if (!isVitestBrowser()) return // `page` and `getCurrentTest` come from a dynamic import; the hooks that // follow read them synchronously, so wait for it here (#835). await whenVitestProxyReady() matcher = autoSnapshotMatcher(commands, expect) const suiteDefaults = toSuiteDefaults(options) if (typeof options?.auto === 'function') { matcherFn = matcher.createMatcher( { auto: options.auto, }, suiteDefaults, ) } else if (typeof options?.auto === 'object') { matcherFn = matcher.createMatcher(options.auto, suiteDefaults) } else matcherFn = matcher.createMatcher({ async auto() {} }, suiteDefaults) await matcher.setup() await setAutoSnapshotOptions(true) }, afterEach() { if (matcherFn) { return matcherFn() } }, ...visAnnotations, }) }