UNPKG

@augment-vir/test

Version:

A universal testing suite that works with Mocha style test runners _and_ Node.js's built-in test runner.

35 lines (34 loc) 1.54 kB
import { getOrSet, randomString, } from '@augment-vir/common'; import { globalElementStoreKey, ScreenshotCommand, } from '@virmator/test/dist/web-screenshot-plugin/screenshot-payload.js'; import { executeServerCommand } from '@web/test-runner-commands'; import { extractTestNameAsDir, } from '../augments/universal-testing-suite/universal-test-context.js'; export async function assertWebScreenshot(element, screenshotNameOrTestContext, options) { if (!element.isConnected) { throw new Error('Element must be connected to the DOM.'); } else if (element.ownerDocument !== document) { throw new Error('Element must belong to the same document the tests are run in.'); } const elementKey = randomString(); getOrSet(globalThis, globalElementStoreKey, () => { return {}; })[elementKey] = element; const screenshotName = typeof screenshotNameOrTestContext === 'string' ? screenshotNameOrTestContext : extractTestNameAsDir(screenshotNameOrTestContext); const payload = { ...options, elementKey, screenshotFileName: screenshotName, }; const result = await executeServerCommand(ScreenshotCommand.CompareScreenshot, payload); if (result.updated) { /** Ignore updates. */ return; } else if (!result.passed) { throw new Error(typeof screenshotNameOrTestContext === 'string' ? `Screenshot comparison failed for '${screenshotNameOrTestContext}'` : 'Screenshot comparison failed.'); } }