UNPKG

@clerk/testing

Version:

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

10 lines (6 loc) 4.48 kB
import{createClerkClient as m}from"@clerk/backend";var _="A secretKey is required to create agent tasks. Pass it directly or set the CLERK_SECRET_KEY environment variable.";var h="Failed to create agent task: ";async function I(r){let{apiUrl:i,secretKey:s,clerkClient:e,...o}=r;if(!e&&!s)throw new Error(_);let n=e??m({apiUrl:i,secretKey:s});try{return await n.agentTasks.create(o)}catch(t){throw new Error(h+(t instanceof Error?t.message:String(t)))}}import{createClerkClient as k}from"@clerk/backend";import{isClerkAPIResponseError as E}from"@clerk/shared/error";import{parsePublishableKey as w}from"@clerk/shared/keys";import C from"dotenv";var p=5,f=1e3,R=500,d=3e4,u=new Set([408,429,500,502,503,504]),S=new Set(["ECONNREFUSED","ECONNRESET","ENOTFOUND","ETIMEDOUT","EAI_AGAIN"]);function g(r){return r instanceof Error&&"code"in r&&S.has(r.code??"")}async function A(r,i){for(let s=0;s<=p;s++)try{return await r()}catch(e){let o=E(e)&&u.has(e.status),n=g(e);if(!o&&!n||s===p)throw e;let t=E(e)?e.status:e.code,c=f*Math.pow(2,s)+Math.random()*R,a=E(e)&&typeof e.retryAfter=="number"?Math.min(Math.max(e.retryAfter*1e3,c),d):Math.min(c,d);console.warn(`[Retry] ${t} for ${i}, attempt ${s+1}/${p}, waiting ${Math.round(a)}ms`),await new Promise(l=>setTimeout(l,a))}throw new Error("Unreachable")}var v=async r=>{let{debug:i=!1,dotenv:s=!0,...e}=r||{},o=a=>{i&&console.log(`Clerk: ${a}`)};o("Setting up Clerk..."),s&&C.config({path:[".env.local",".env"]});let n=e.publishableKey||process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY||process.env.VITE_CLERK_PUBLISHABLE_KEY||process.env.CLERK_PUBLISHABLE_KEY||process.env.REACT_APP_CLERK_PUBLISHABLE_KEY||process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY,t=e.secretKey||process.env.CLERK_SECRET_KEY,c=process.env.CLERK_TESTING_TOKEN;if(!n)throw new Error("You need to set the CLERK_PUBLISHABLE_KEY environment variable.");if(!t&&!c)throw new Error("You need to set the CLERK_SECRET_KEY or the CLERK_TESTING_TOKEN environment variable.");if(t&&!c){o("Fetching testing token from Clerk Backend API...");try{let a=e?.apiUrl||process.env.CLERK_API_URL,l=k({secretKey:t,apiUrl:a});c=(await A(()=>l.testingTokens.createTestingToken(),"testingTokens.createTestingToken")).token}catch(a){throw console.error("Failed to fetch testing token from Clerk API."),a}}return{CLERK_FAPI:r?.frontendApiUrl||w(n)?.frontendApi,CLERK_TESTING_TOKEN:c}};var U=async({signInParams:r,windowObject:i})=>{try{let s=i||window;if(!s.Clerk.client)return;let e=s.Clerk.client.signIn;switch(r.strategy){case"password":{let o=await e.create(r);await s.Clerk.setActive({session:o.createdSessionId});break}case"ticket":{let o=await e.create({strategy:"ticket",ticket:r.ticket});if(o.status==="complete")await s.Clerk.setActive({session:o.createdSessionId});else throw new Error(`Sign-in with ticket failed. Status: ${o.status}`);break}case"phone_code":{if(!/^\+1\d{3}55501\d{2}$/.test(r.identifier))throw new Error(`Phone number should be a test phone number. Example: +1XXX55501XX. Learn more here: https://clerk.com/docs/testing/test-emails-and-phones#phone-numbers`);let{supportedFirstFactors:o}=await e.create({identifier:r.identifier}),n=o?.find(t=>t.strategy==="phone_code");if(n){await e.prepareFirstFactor({strategy:"phone_code",phoneNumberId:n.phoneNumberId});let t=await e.attemptFirstFactor({strategy:"phone_code",code:"424242"});if(t.status==="complete")await s.Clerk.setActive({session:t.createdSessionId});else throw new Error(`Status is ${t.status}`)}else throw new Error("phone_code is not enabled.");break}case"email_code":{if(!r.identifier.includes("+clerk_test"))throw new Error(`Email should be a test email. Any email with the +clerk_test subaddress is a test email address. Learn more here: https://clerk.com/docs/testing/test-emails-and-phones#email-addresses`);let{supportedFirstFactors:o}=await e.create({identifier:r.identifier}),n=o?.find(t=>t.strategy==="email_code");if(n){await e.prepareFirstFactor({strategy:"email_code",emailAddressId:n.emailAddressId});let t=await e.attemptFirstFactor({strategy:"email_code",code:"424242"});if(t.status==="complete")await s.Clerk.setActive({session:t.createdSessionId});else throw new Error(`Status is ${t.status}`)}else throw new Error("email_code is not enabled.");break}default:throw new Error(`Unsupported strategy: ${r.strategy}`)}}catch(s){throw new Error(`Clerk: Failed to sign in: ${s?.message}`)}};export{I as a,v as b,U as c}; //# sourceMappingURL=chunk-I4TOP4AO.mjs.map