UNPKG

@clerk/testing

Version:

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

10 lines (6 loc) 3.26 kB
import{createClerkClient as l}from"@clerk/backend";import{parsePublishableKey as d}from"@clerk/shared/keys";import p from"dotenv";var k=async s=>{let{debug:c=!1,dotenv:o=!0,...t}=s||{},r=i=>{c&&console.log(`Clerk: ${i}`)};r("Setting up Clerk..."),o&&p.config({path:[".env.local",".env"]});let n=t.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,e=t.secretKey||process.env.CLERK_SECRET_KEY,a=process.env.CLERK_TESTING_TOKEN;if(!n)throw new Error("You need to set the CLERK_PUBLISHABLE_KEY environment variable.");if(!e&&!a)throw new Error("You need to set the CLERK_SECRET_KEY or the CLERK_TESTING_TOKEN environment variable.");if(e&&!a){r("Fetching testing token from Clerk Backend API...");try{let i=t?.apiUrl||process.env.CLERK_API_URL;a=(await l({secretKey:e,apiUrl:i}).testingTokens.createTestingToken()).token}catch(i){throw console.error("Failed to fetch testing token from Clerk API."),i}}return{CLERK_FAPI:s?.frontendApiUrl||d(n)?.frontendApi,CLERK_TESTING_TOKEN:a}};var f=async({signInParams:s,windowObject:c})=>{try{let o=c||window;if(!o.Clerk.client)return;let t=o.Clerk.client.signIn;switch(s.strategy){case"password":{let r=await t.create(s);await o.Clerk.setActive({session:r.createdSessionId});break}case"ticket":{let r=await t.create({strategy:"ticket",ticket:s.ticket});if(r.status==="complete")await o.Clerk.setActive({session:r.createdSessionId});else throw new Error(`Sign-in with ticket failed. Status: ${r.status}`);break}case"phone_code":{if(!/^\+1\d{3}55501\d{2}$/.test(s.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:r}=await t.create({identifier:s.identifier}),n=r?.find(e=>e.strategy==="phone_code");if(n){await t.prepareFirstFactor({strategy:"phone_code",phoneNumberId:n.phoneNumberId});let e=await t.attemptFirstFactor({strategy:"phone_code",code:"424242"});if(e.status==="complete")await o.Clerk.setActive({session:e.createdSessionId});else throw new Error(`Status is ${e.status}`)}else throw new Error("phone_code is not enabled.");break}case"email_code":{if(!s.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:r}=await t.create({identifier:s.identifier}),n=r?.find(e=>e.strategy==="email_code");if(n){await t.prepareFirstFactor({strategy:"email_code",emailAddressId:n.emailAddressId});let e=await t.attemptFirstFactor({strategy:"email_code",code:"424242"});if(e.status==="complete")await o.Clerk.setActive({session:e.createdSessionId});else throw new Error(`Status is ${e.status}`)}else throw new Error("email_code is not enabled.");break}default:throw new Error(`Unsupported strategy: ${s.strategy}`)}}catch(o){throw new Error(`Clerk: Failed to sign in: ${o?.message}`)}};export{k as a,f as b}; //# sourceMappingURL=chunk-XQ3MBH5Q.mjs.map