@clerk/testing
Version:
Utilities to help you create E2E test suites for apps using Clerk
4 lines • 2.15 kB
JavaScript
import{a as g,b as w}from"./chunk-M5YIJ3SE.mjs";var l=new WeakSet,C=new Set([429,502,503,504]),n=3,E=500,h=250,k=async({context:y,options:_,page:d})=>{let i=y??d?.context();if(!i)throw new Error("Either context or page must be provided to setup testing token");if(l.has(i)){process.env.CLERK_TESTING_DEBUG&&console.log("[Clerk Testing] Route handler already registered for this context, skipping duplicate setup");return}let p=_?.frontendApiUrl||process.env.CLERK_FAPI;if(!p)throw new Error(w);let m=p.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),$=new RegExp(`^https://${m}/v1/.*?(\\?.*)?$`);l.add(i);try{await i.route($,async e=>{let u=new URL(e.request().url()),T=process.env.CLERK_TESTING_TOKEN;T&&u.searchParams.set(g,T);let f=u.toString();for(let r=0;r<=n;r++)try{let t=await e.fetch({url:f}),o=t.status();if(C.has(o)){if(r<n){let a=E*Math.pow(2,r)+Math.random()*h;process.env.CLERK_TESTING_DEBUG&&console.log(`[Clerk Testing] FAPI returned ${o}, retrying (attempt ${r+1}/${n}, delay ${Math.round(a)}ms): ${e.request().url()}`),await new Promise(S=>setTimeout(S,a));continue}let c="";try{let a=t.headers();c=`
cf-ray: ${a["cf-ray"]??"n/a"}, retry-after: ${a["retry-after"]??"n/a"}, content-type: ${a["content-type"]??"n/a"}`,c+=`
body: ${(await t.text()).slice(0,500)}`}catch{}console.warn(`[Clerk Testing] FAPI request failed with status ${o} after ${n+1} attempts: ${e.request().url()}${c}`),await e.fulfill({response:t});return}let s=await t.json();s?.response?.captcha_bypass===!1&&(s.response.captcha_bypass=!0),s?.client?.captcha_bypass===!1&&(s.client.captcha_bypass=!0),await e.fulfill({response:t,json:s});return}catch(t){if(r<n){let o=E*Math.pow(2,r)+Math.random()*h;process.env.CLERK_TESTING_DEBUG&&console.log(`[Clerk Testing] FAPI request error, retrying (attempt ${r+1}/${n}, delay ${Math.round(o)}ms): ${e.request().url()}`,t),await new Promise(s=>setTimeout(s,o));continue}console.warn(`[Clerk Testing] FAPI request failed after ${n+1} attempts: ${e.request().url()} (${String(t)})`),await e.continue({url:f}).catch(console.error);return}})}catch(e){throw l.delete(i),e}};export{k as a};
//# sourceMappingURL=chunk-6QEJXXZK.mjs.map