@lewiswright/vitest-plugin-vis
Version:
Vitest visual testing plugin
34 lines (33 loc) • 1.52 kB
JavaScript
import { resolve } from 'pathe';
import { isBase64String } from "../../shared/base64.js";
import { file } from "../file.js";
import { takeSnapshot, takeSnapshotByBrowser, writeSnapshot } from "../snapshot.js";
import { visContext } from "../vis_context.js";
import { assertTestPathDefined } from "./_assertions.js";
export const prepareImageSnapshotComparison = async (context, taskId, subject, isAutoSnapshot, options) => {
assertTestPathDefined(context, 'prepareImageSnapshotComparison');
// vitest:browser passes in `null` when not defined
if (!options)
options = {};
options.timeout = options.timeout ?? 30000;
options.animations = options.animations ?? true;
const projectRoot = context.project.config.root;
const info = await visContext.getSnapshotInfo(context, taskId, isAutoSnapshot, options);
const baselineBuffer = await file.tryReadFile(resolve(projectRoot, info.baselinePath));
if (!baselineBuffer) {
if (isBase64String(subject)) {
await writeSnapshot(resolve(projectRoot, info.baselinePath), subject);
}
else {
await takeSnapshotByBrowser(context, resolve(projectRoot, info.baselinePath), subject, options);
}
return;
}
const resultBuffer = await takeSnapshot(context, resolve(projectRoot, info.resultPath), subject, options);
return {
...info,
projectRoot,
baseline: baselineBuffer.toString('base64'),
result: resultBuffer.toString('base64'),
};
};