@lewiswright/vitest-plugin-vis
Version:
Vitest visual testing plugin
25 lines (24 loc) • 1.03 kB
JavaScript
import { commands } from '@vitest/browser/context';
import { ctx } from "../ctx.js";
import { toTaskId } from "../task_id.js";
/**
* Check if the snapshot image exists.
*/
export function hasImageSnapshot(options) {
const test = ctx.getCurrentTest();
if (!test) {
throw new Error('`hasImageSnapshot()` must be called in a test.');
}
if (test.concurrent) {
throw new Error('`hasImageSnapshot()` cannot be called in a concurrent test because ' +
"concurrent tests run at the same time in the same iframe and affect each other's environment. ");
}
const taskId = toTaskId(test);
const isAutoSnapshot = !!test.meta.vis?.isAutoSnapshot;
if (options?.customizeSnapshotId) {
return commands
.imageSnapshotNextIndex(taskId)
.then((index) => commands.hasImageSnapshot(taskId, options.customizeSnapshotId({ id: taskId, index, isAutoSnapshot }), isAutoSnapshot));
}
return commands.hasImageSnapshot(taskId, undefined, isAutoSnapshot);
}