UNPKG

vitest-plugin-vis

Version:
1 lines 4.85 kB
{"version":3,"file":"auto_snapshot_options-DUOTpF2N.mjs","names":["vitestSuite: Awaited<typeof import('vitest/suite')>","list: any[]"],"sources":["../src/client/external/vitest/vitest_suite_proxy.ts","../src/client/task/_ctx.ts","../src/client/task/auto_snapshot_options.ts"],"sourcesContent":["import type { SnapshotTestMeta } from '../../../shared/types.ts'\n\nlet vitestSuite: Awaited<typeof import('vitest/suite')>\n\nif ((globalThis as any).__vitest_browser__) {\n\timport('vitest/suite').then((m) => {\n\t\tvitestSuite = m\n\t})\n}\n\nexport type CurrentTest = ReturnType<typeof vitestSuite.getCurrentTest> & SnapshotTestMeta\n\nexport const getCurrentTest = () => vitestSuite?.getCurrentTest() as CurrentTest\nexport const getCurrentSuite = () => vitestSuite?.getCurrentSuite()\n","import { getCurrentSuite, getCurrentTest } from '../external/vitest/vitest_suite_proxy.ts'\n\nexport const ctx = {\n\tgetCurrentTest,\n\tgetCurrentSuite,\n\t__test__reset() {\n\t\tctx.getCurrentTest = getCurrentTest\n\t\tctx.getCurrentSuite = getCurrentSuite\n\t},\n}\n","import { assertSnapshotKeyWithoutDash } from '../../shared/asserts.ts'\nimport { NAME } from '../../shared/constants.ts'\nimport type { ComparisonMethod } from '../../shared/types.ts'\nimport { ctx } from './_ctx.ts'\nimport type { SnapshotMeta } from './snapshot_meta.ts'\n\ntype Suite = { meta: Record<string, any>; suite?: Suite | undefined }\n\nexport type MetaTask =\n\t| {\n\t\t\tfile?: { meta: Record<string, any> } | undefined\n\t\t\tsuite?: Suite | undefined\n\t\t\tmeta: Record<string, any>\n\t }\n\t| undefined\n\n/**\n * Set the snapshot options for auto snapshot.\n *\n * You can use it in beforeAll, beforeEach, or individual test cases.\n *\n * @param meta - The snapshot options to set.\n * You can pass a boolean to enable/disable the snapshot, or an object with the snapshot options.\n * When passing an object, the `enable` property will be set to `true` by default unless you explicitly set it to `false`.\n * @param meta.enable - Whether to enable the snapshot.\n * @param meta.snapshotKey - The key to use for the snapshot.\n * @param meta.comparisonMethod - The comparison method to use for the snapshot.\n * @param meta.diffOptions - The diff options to use for the snapshot.\n * @param meta.failureThreshold - The failure threshold to use for the snapshot.\n * @param meta.failureThresholdType - The failure threshold type to use for the snapshot.\n * @param meta.timeout - The timeout to use for the snapshot.\n *\n * @example\n *\n * ```ts\n * beforeAll(() => setAutoSnapshotOptions(...))\n * beforeEach(() => setAutoSnapshotOptions(...))\n *\n * it('...', () => {\n * setAutoSnapshotOptions(...)\n * })\n * ```\n */\nexport function setAutoSnapshotOptions<M extends ComparisonMethod>(meta: boolean | SnapshotMeta<M>): void {\n\tif (typeof meta === 'object') {\n\t\tassertSnapshotKeyWithoutDash(meta.snapshotKey)\n\t}\n\n\tconst task = getTask()\n\tif (!task) return\n\n\ttask.meta[NAME] = {\n\t\t...task.meta[NAME],\n\t\t...parseMeta(meta),\n\t}\n}\n\nfunction getTask(): MetaTask | undefined {\n\treturn ctx.getCurrentTest() ?? (ctx.getCurrentSuite()?.tasks?.[0] as any)?.file\n}\n\nfunction parseMeta<M extends ComparisonMethod>(meta: boolean | SnapshotMeta<M>): SnapshotMeta<M> {\n\treturn typeof meta === 'boolean' ? ({ enable: meta } as any) : { enable: true, ...meta }\n}\n\nexport function extractAutoSnapshotOptions<M extends SnapshotMeta<any> = SnapshotMeta<any>>(\n\ttask: MetaTask,\n): M | undefined {\n\tif (!task) return\n\n\tconst list: any[] = []\n\tlet current = task\n\twhile (current?.suite) {\n\t\tlist.unshift(current.suite.meta)\n\t\tcurrent = current.suite\n\t}\n\tlist.unshift(task.file?.meta)\n\tlist.push(task.meta)\n\treturn list.reduce(\n\t\t(acc, cur) => {\n\t\t\tconst meta = cur?.[NAME]\n\t\t\treturn meta ? Object.assign({}, acc, meta) : acc\n\t\t},\n\t\t{ enable: false },\n\t)\n}\n"],"mappings":";;;;AAEA,IAAIA;AAEJ,IAAK,WAAmB,mBACvB,QAAO,gBAAgB,MAAM,MAAM;AAClC,eAAc;EACb;AA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