UNPKG

@clerk/testing

Version:

Utilities to help you create E2E test suites for apps using Clerk

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