@storycap-testrun/browser
Version:
Screenshot capture for Storybook addon-vitest - Provides stable visual testing functionality and Vitest plugin for Storybook stories running in browser environments
8 lines • 3.58 kB
JavaScript
const e=require(`./chunk-nOFOJqeH.js`),t=e.__toESM(require(`@vitest/browser/context`)),n=e.__toESM(require(`@storycap-testrun/internal`)),r=()=>({async setup(e){let t=document.createElement(`style`);t.innerHTML=`
*,
*::before,
*::after {
transition: none !important;
animation: none !important;
}
`,t.id=`::x-storycap-animations`,document.head.appendChild(t)},async postCapture(e){document.getElementById(`::x-storycap-animations`)?.remove()}}),i=async e=>{document.querySelectorAll(e).forEach(e=>{e.remove()})},a=e=>({async setup(t){await i(e)},async preCapture(t){await i(e)}}),o=e=>({async preCapture(t){let n=e.selector,r=e.color;document.querySelectorAll(n).forEach(e=>{let t=e.getBoundingClientRect(),n=document.createElement(`x-storycap-mask`);n.style.position=`absolute`,n.style.top=`${t.top}px`,n.style.left=`${t.left}px`,n.style.zIndex=`calc(infinity)`,n.style.display=`block`,n.style.width=`${Math.ceil(t.width)}px`,n.style.height=`${Math.ceil(t.height)}px`,n.style.pointerEvents=`none`,n.style.background=r,document.body.appendChild(n)})},postCapture:async e=>{document.querySelectorAll(`x-storycap-mask`).forEach(e=>{e.remove()})}}),s=async(e,r)=>{let i=(0,t.cdp)();await(0,n.waitForStableMetrics)(i,r,e.id)},c=()=>document.readyState===`loading`?new Promise(e=>{document.addEventListener(`DOMContentLoaded`,()=>{e()},{once:!0})}):Promise.resolve(),l=()=>document.readyState===`complete`?Promise.resolve():new Promise(e=>{window.addEventListener(`load`,()=>{e()},{once:!0})}),u=(e=500)=>new Promise(t=>{let n,r=new PerformanceObserver(i=>{let a=i.getEntries();a.length>0&&(n!==void 0&&clearTimeout(n),n=window.setTimeout(()=>{r.disconnect(),t()},e))});r.observe({entryTypes:[`resource`]}),n=window.setTimeout(()=>{r.disconnect(),t()},e)}),d=async()=>{await c(),await l(),await u(),await document.fonts.ready},f=async(e,{enabled:n,retries:r})=>{let i=t.server.browser;n&&i===`chromium`?(await d(),await s(e,r)):(n&&console.warn(`Prevention of flakiness through metrics is supported only in Chromium`),await d())};function p(e){if(e==null||typeof e!=`function`)throw Error(`The test context is not an object. Make sure to run the test in a Storybook environment.`);if(!(`story`in e))throw Error(`The test context does not contain story information. Make sure to run the test in a Storybook environment.`);if(!(`id`in e.story))throw Error("The test context story information does not contain an `id`.");if(!(`storyName`in e.story))throw Error("The test context story information does not contain a `storyName`.");if(!(`parameters`in e.story))throw Error("The test context story information does not contain `parameters`.")}const m=()=>({createContext:e=>({id:e.story.id,file:e.task.file.name,name:e.story.storyName}),getParameters:(e,t)=>t.story.parameters?.screenshot??{},resolveFilepath:async e=>t.commands.resolveScreenshotFilepath(e),createHash:async e=>{let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);let r=await window.crypto.subtle.digest(`SHA-256`,n);return Array.from(new Uint8Array(r)).map(e=>e.toString(16).padStart(2,`0`)).join(``)},waitForStable:async(e,t,n)=>{await f(t,n)},takeScreenshot:async(e,t,n)=>{let r=await e.screenshot({path:t,save:!0,base64:!0,animations:`disabled`,caret:`hide`,fullPage:n.fullPage,omitBackground:n.omitBackground,scale:n.scale,type:n.type});return r.base64},createAnimationsHook:r,createRemovalHook:a,createMaskingHook:o}),h=async(e,t,r={})=>{p(t);let i=m(),a=(0,n.createScreenshotFunction)(i);await a(e,t,r)};exports.screenshot=h;