UNPKG

@clerk/testing

Version:

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

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