vitest-plugin-vis
Version:
Vitest visual testing plugin
1 lines • 11.7 kB
Source Map (JSON)
{"version":3,"file":"setup-DCytWz48.mjs","names":["ctx","toMatchImageSnapshot","ctx","commands"],"sources":["../src/client/expect/_ctx.ts","../src/client/expect/to_match_image_snapshot.ts","../src/client/expect/extend.ts","../src/client/page/_ctx.ts","../src/client/page/has_image_snapshot.ts","../src/client/page/to_match_image_snapshot.ts","../src/client/page/extend.ts","../src/setup/create_vis.ts","../src/setup/vis.ts"],"sourcesContent":["import { commands } from 'vitest/browser'\nimport { getCurrentTest } from '../external/vitest/vitest_suite_proxy.ts'\n\nexport const ctx = {\n\tcommands,\n\tgetCurrentTest,\n\t__test__reset() {\n\t\tctx.getCurrentTest = getCurrentTest\n\t},\n}\n","import type { AsyncExpectationResult } from '@vitest/expect'\nimport type { ToMatchImageSnapshotOptions } from '../../shared/types.ts'\nimport { matchImageSnapshotAction } from '../actions/match_image_snapshot_action.ts'\nimport { toTaskId } from '../task/task_id.ts'\nimport { ctx } from './_ctx.ts'\nimport { success } from './expectation_result.ts'\n\nexport function toMatchImageSnapshot(\n\t/**\n\t * The element or locator to take the snapshot of,\n\t * or the base64 value of the image to compare against.\n\t */\n\tsubject: any,\n\toptions?: ToMatchImageSnapshotOptions | undefined,\n): AsyncExpectationResult {\n\tconst test = ctx.getCurrentTest()\n\tif (!test) {\n\t\tthrow new Error('`toMatchImageSnapshot()` must be called in a test.')\n\t}\n\n\tif (test.concurrent) {\n\t\tthrow new Error(\n\t\t\t'`toMatchImageSnapshot()` cannot be called in a concurrent test because ' +\n\t\t\t\t\"concurrent tests run at the same time in the same iframe and affect each other's environment.\",\n\t\t)\n\t}\n\n\treturn matchImageSnapshotAction(ctx.commands, toTaskId(test), subject, options).then(() => success)\n}\n","import { expect } from 'vitest'\nimport { toMatchImageSnapshot } from './to_match_image_snapshot.ts'\nimport './augment.ts'\n\nexpect.extend({ toMatchImageSnapshot })\n","import { type BrowserPage, commands } from 'vitest/browser'\nimport { getCurrentTest } from '../external/vitest/vitest_suite_proxy.ts'\n\nexport type { BrowserPage }\n\nexport const ctx = {\n\tcommands,\n\tgetCurrentTest,\n\t__test__reset() {\n\t\tctx.getCurrentTest = getCurrentTest\n\t},\n}\n","import type { ImageSnapshotKeyOptions } from '../../shared/types.ts'\nimport { hasImageSnapshotAction } from '../actions/has_image_snapshot_action.ts'\nimport { toTaskId } from '../task/task_id.ts'\nimport type { BrowserPage } from './_ctx.ts'\nimport { ctx } from './_ctx.ts'\n\nexport interface HasImageSnapshotAction {\n\t/**\n\t * Check if the snapshot image exists.\n\t */\n\thasImageSnapshot(this: BrowserPage, options?: ImageSnapshotKeyOptions | undefined): Promise<boolean>\n}\n\nexport function hasImageSnapshot(this: BrowserPage, options?: ImageSnapshotKeyOptions | undefined) {\n\tconst test = ctx.getCurrentTest()\n\tif (!test) {\n\t\tthrow new Error('`hasImageSnapshot()` must be called in a test.')\n\t}\n\n\tif (test.concurrent) {\n\t\tthrow new Error(\n\t\t\t'`hasImageSnapshot()` cannot be called in a concurrent test because ' +\n\t\t\t\t\"concurrent tests run at the same time in the same iframe and affect each other's environment. \",\n\t\t)\n\t}\n\treturn hasImageSnapshotAction(ctx.commands, toTaskId(test), options)\n}\n","import type { ComparisonMethod, ToMatchPageImageSnapshotOptions } from '../../shared/types.ts'\nimport { matchPageImageSnapshotAction } from '../actions/match_page_image_snapshot_action.ts'\nimport { toTaskId } from '../task/task_id.ts'\nimport { ctx } from './_ctx.ts'\n\nexport interface ToMatchImageSnapshotAction {\n\ttoMatchImageSnapshot<M extends ComparisonMethod = 'pixel'>(\n\t\toptions?: ToMatchPageImageSnapshotOptions<M> | undefined,\n\t): Promise<void>\n}\n\nexport function toMatchImageSnapshot<M extends ComparisonMethod = 'pixel'>(\n\toptions?: ToMatchPageImageSnapshotOptions<M> | undefined,\n) {\n\tconst test = ctx.getCurrentTest()\n\tif (!test) {\n\t\tthrow new Error('`toMatchImageSnapshot()` must be called in a test.')\n\t}\n\n\tif (test.concurrent) {\n\t\tthrow new Error(\n\t\t\t'`toMatchImageSnapshot()` cannot be called in a concurrent test because ' +\n\t\t\t\t\"concurrent tests run at the same time in the same iframe and affect each other's environment.\",\n\t\t)\n\t}\n\n\treturn matchPageImageSnapshotAction(ctx.commands, toTaskId(test), options)\n}\n","import { page } from 'vitest/browser'\nimport './augment.ts'\nimport { hasImageSnapshot } from './has_image_snapshot.ts'\nimport { toMatchImageSnapshot } from './to_match_image_snapshot.ts'\n\npage.extend({ hasImageSnapshot, toMatchImageSnapshot })\n","import { afterEach, beforeAll, expect } from 'vitest'\nimport { autoSnapshotMatcher } from '../client/suite/auto_snapshot_matcher.ts'\nimport { setAutoSnapshotOptions } from '../client/task/auto_snapshot_options.ts'\nimport type { SnapshotMeta } from '../client/task/snapshot_meta.ts'\nimport type { SetupVisSuiteCommand } from '../shared/commands.types.ts'\nimport type { ComparisonMethod, SetupVisOptions } from '../shared/types.ts'\n\n/**\n * Visual test configuration on the client side.\n */\nexport type VisClientConfigurator<GM extends Record<string, any> | unknown = unknown> = {\n\t/**\n\t * Setup the visual test configuration.\n\t *\n\t * @example\n\t * ```ts\n\t * // Setup with auto snapshot enabled\n\t * vis().setup({ auto: true })\n\t *\n\t * // Setup with auto snapshot disabled\n\t * vis().setup({ auto: false })\n\t *\n\t * // Same as `vis.setup({ auto: false })`\n\t * vis.setup()\n\t *\n\t * // Setup with auto snapshot determined by the test meta\n\t * vis.setup({\n\t * \tauto: async ({ meta }) => meta['darkOnly'],\n\t * })\n\t *\n\t * // Setup with multiple auto snapshots\n\t * vis.setup({\n\t * auto: {\n\t * async light() { document.body.classList.remove('dark') },\n\t * async dark() { document.body.classList.add('dark') },\n\t * }\n\t * })\n\t * ```\n\t */\n\tsetup(options?: SetupVisOptions<GM>): void\n\t/**\n\t * @deprecated Use `vis.setup()` instead.\n\t */\n\tpresets: {\n\t\t/**\n\t\t * @deprecated Use `vis.setup()` instead.\n\t\t *\n\t\t * Enable visual testing.\n\t\t *\n\t\t * auto snapshot is turned off by default.\n\t\t * You can specify the test to take a snapshot during `afterEach()` hook with `setAutoSnapshotOptions()`.\n\t\t */\n\t\tenable(): void\n\t\t/**\n\t\t * @deprecated Use `vis.setup()` instead.\n\t\t *\n\t\t * Enable visual testing.\n\t\t *\n\t\t * `setAutoSnapshotOptions` will have no effect in this preset.\n\t\t */\n\t\tmanual(): void\n\t\t/**\n\t\t * @deprecated Use `vis.setup()` instead.\n\t\t *\n\t\t * Enable automatic visual testing.\n\t\t *\n\t\t * This will take a snapshot after each test.\n\t\t */\n\t\tauto(): void\n\t\t/**\n\t\t * @deprecated Use `vis.setup()` instead.\n\t\t *\n\t\t * Enable automatic visual testing with multiple themes.\n\t\t *\n\t\t * This will take a snapshot after each test for each theme.\n\t\t *\n\t\t * @param themes A record of theme names and their setup functions.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * vis().presets.theme({\n\t\t * light() { document.body.classList.add('light') },\n\t\t * dark() { document.body.classList.add('dark') },\n\t\t * })\n\t\t * ```\n\t\t */\n\t\ttheme<C extends ComparisonMethod, M extends Record<string, any> | unknown = unknown>(\n\t\t\tthemes: Record<\n\t\t\t\tstring,\n\t\t\t\tboolean | ((options: SnapshotMeta<C> & M & GM) => Promise<boolean> | Promise<void> | boolean | void)\n\t\t\t>,\n\t\t): void\n\t}\n\tbeforeAll: {\n\t\tsetup(): Promise<void>\n\t}\n\tafterEach: {\n\t\tmatchImageSnapshot(): Promise<void>\n\t\tmatchPerTheme<C extends ComparisonMethod, M extends Record<string, any> | unknown = unknown>(\n\t\t\tthemes: Record<\n\t\t\t\tstring,\n\t\t\t\tboolean | ((options: SnapshotMeta<C> & M & GM) => Promise<boolean> | Promise<void> | boolean | void)\n\t\t\t>,\n\t\t): () => Promise<void>\n\t}\n}\n\nexport function createVis<GM extends Record<string, any> | unknown = unknown>(commands: SetupVisSuiteCommand) {\n\tconst matcher = autoSnapshotMatcher(commands, expect)\n\n\tconst vis: VisClientConfigurator<GM> = {\n\t\tsetup(options) {\n\t\t\tif (!options || options.auto === false) {\n\t\t\t\tbeforeAll(matcher.setup)\n\t\t\t} else {\n\t\t\t\tbeforeAll(async () => {\n\t\t\t\t\tawait matcher.setup()\n\t\t\t\t\tsetAutoSnapshotOptions(true)\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tif (typeof options?.auto === 'function') {\n\t\t\t\tafterEach(\n\t\t\t\t\tmatcher.createMatcher({\n\t\t\t\t\t\tauto: options.auto,\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t\t} else if (typeof options?.auto === 'object') {\n\t\t\t\tafterEach(matcher.createMatcher(options.auto))\n\t\t\t} else {\n\t\t\t\tafterEach(matcher.createMatcher({ async auto() {} }))\n\t\t\t}\n\t\t},\n\t\tpresets: {\n\t\t\tenable() {\n\t\t\t\tbeforeAll(matcher.setup)\n\t\t\t\tafterEach(matcher.createMatcher({ async auto() {} }))\n\t\t\t},\n\t\t\tmanual() {\n\t\t\t\tbeforeAll(matcher.setup)\n\t\t\t},\n\t\t\tauto() {\n\t\t\t\tbeforeAll(async () => {\n\t\t\t\t\tawait matcher.setup()\n\t\t\t\t\tsetAutoSnapshotOptions(true)\n\t\t\t\t})\n\t\t\t\tafterEach(matcher.createMatcher({ async auto() {} }))\n\t\t\t},\n\t\t\ttheme(themes) {\n\t\t\t\tbeforeAll(async () => {\n\t\t\t\t\tawait matcher.setup()\n\t\t\t\t\tsetAutoSnapshotOptions(true)\n\t\t\t\t})\n\t\t\t\tafterEach(matcher.createMatcher(themes))\n\t\t\t},\n\t\t},\n\t\tbeforeAll: {\n\t\t\tasync setup() {\n\t\t\t\tawait matcher.setup()\n\t\t\t},\n\t\t},\n\t\tafterEach: {\n\t\t\tasync matchImageSnapshot() {\n\t\t\t\treturn matcher.createMatcher({ async auto() {} })()\n\t\t\t},\n\t\t\tmatchPerTheme(themes) {\n\t\t\t\treturn matcher.createMatcher(themes)\n\t\t\t},\n\t\t},\n\t}\n\treturn vis\n}\n","import { commands } from 'vitest/browser'\nimport { createVis } from './create_vis.ts'\n\n/**\n * Visual test configuration on the client side.\n */\nexport const vis = createVis(commands)\n"],"mappings":";;;;;;AAGA,MAAaA,QAAM;CAClB;CACA;CACA,gBAAgB;AACf,QAAI,iBAAiB;;CAEtB;;;;ACFD,SAAgBC,uBAKf,SACA,SACyB;CACzB,MAAM,OAAOC,MAAI,gBAAgB;AACjC,KAAI,CAAC,KACJ,OAAM,IAAI,MAAM,qDAAqD;AAGtE,KAAI,KAAK,WACR,OAAM,IAAI,MACT,uKAEA;AAGF,QAAO,yBAAyBA,MAAI,UAAU,SAAS,KAAK,EAAE,SAAS,QAAQ,CAAC,WAAW,QAAQ;;;;;ACvBpG,OAAO,OAAO,EAAE,8CAAsB,CAAC;;;;ACCvC,MAAa,MAAM;CAClB;CACA;CACA,gBAAgB;AACf,MAAI,iBAAiB;;CAEtB;;;;ACED,SAAgB,iBAAoC,SAA+C;CAClG,MAAM,OAAO,IAAI,gBAAgB;AACjC,KAAI,CAAC,KACJ,OAAM,IAAI,MAAM,iDAAiD;AAGlE,KAAI,KAAK,WACR,OAAM,IAAI,MACT,oKAEA;AAEF,QAAO,uBAAuB,IAAI,UAAU,SAAS,KAAK,EAAE,QAAQ;;;;;ACdrE,SAAgB,qBACf,SACC;CACD,MAAM,OAAO,IAAI,gBAAgB;AACjC,KAAI,CAAC,KACJ,OAAM,IAAI,MAAM,qDAAqD;AAGtE,KAAI,KAAK,WACR,OAAM,IAAI,MACT,uKAEA;AAGF,QAAO,6BAA6B,IAAI,UAAU,SAAS,KAAK,EAAE,QAAQ;;;;;ACrB3E,KAAK,OAAO;CAAE;CAAkB;CAAsB,CAAC;;;;ACsGvD,SAAgB,UAA8D,YAAgC;CAC7G,MAAM,UAAU,oBAAoBC,YAAU,OAAO;AA8DrD,QA5DuC;EACtC,MAAM,SAAS;AACd,OAAI,CAAC,WAAW,QAAQ,SAAS,MAChC,WAAU,QAAQ,MAAM;OAExB,WAAU,YAAY;AACrB,UAAM,QAAQ,OA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