vitest-plugin-vis
Version:
Vitest visual testing plugin
88 lines (84 loc) • 2.77 kB
JavaScript
import { t as assertSnapshotKeyWithoutDash } from "./asserts--HSSlgPD.mjs";
import { r as NAME } from "./constants-CTBaz8Ml.mjs";
//#region src/client/external/vitest/vitest_suite_proxy.ts
let vitestSuite;
if (globalThis.__vitest_browser__) import("vitest/suite").then((m) => {
vitestSuite = m;
});
const getCurrentTest = () => vitestSuite?.getCurrentTest();
const getCurrentSuite = () => vitestSuite?.getCurrentSuite();
//#endregion
//#region src/client/task/_ctx.ts
const ctx = {
getCurrentTest,
getCurrentSuite,
__test__reset() {
ctx.getCurrentTest = getCurrentTest;
ctx.getCurrentSuite = getCurrentSuite;
}
};
//#endregion
//#region src/client/task/auto_snapshot_options.ts
/**
* 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(...)
* })
* ```
*/
function setAutoSnapshotOptions(meta) {
if (typeof meta === "object") assertSnapshotKeyWithoutDash(meta.snapshotKey);
const task = getTask();
if (!task) return;
task.meta[NAME] = {
...task.meta[NAME],
...parseMeta(meta)
};
}
function getTask() {
return ctx.getCurrentTest() ?? (ctx.getCurrentSuite()?.tasks?.[0])?.file;
}
function parseMeta(meta) {
return typeof meta === "boolean" ? { enable: meta } : {
enable: true,
...meta
};
}
function extractAutoSnapshotOptions(task) {
if (!task) return;
const list = [];
let current = task;
while (current?.suite) {
list.unshift(current.suite.meta);
current = current.suite;
}
list.unshift(task.file?.meta);
list.push(task.meta);
return list.reduce((acc, cur) => {
const meta = cur?.[NAME];
return meta ? Object.assign({}, acc, meta) : acc;
}, { enable: false });
}
//#endregion
export { setAutoSnapshotOptions as n, getCurrentTest as r, extractAutoSnapshotOptions as t };
//# sourceMappingURL=auto_snapshot_options-DUOTpF2N.mjs.map