@clerk/testing
Version:
Utilities to help you create E2E test suites for apps using Clerk
10 lines (6 loc) • 3 kB
JavaScript
var _="__clerk_testing_token";import{createClerkClient as l}from"@clerk/backend";import{isProductionFromSecretKey as p,parsePublishableKey as d}from"@clerk/shared/keys";import E from"dotenv";var m=async e=>{let i=s=>{e?.debug&&console.log(`Clerk: ${s}`)};i("Setting up Clerk..."),E.config({path:[".env.local",".env"]});let t=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,r=process.env.CLERK_SECRET_KEY,o=process.env.CLERK_TESTING_TOKEN;if(!t)throw new Error("You need to set the CLERK_PUBLISHABLE_KEY environment variable.");if(!r&&!o)throw new Error("You need to set the CLERK_SECRET_KEY or the CLERK_TESTING_TOKEN environment variable.");if(r&&!o){if(p(r))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 s=process.env.CLERK_API_URL;o=(await l({secretKey:r,apiUrl:s}).testingTokens.createTestingToken()).token}catch(s){throw console.error("Failed to fetch testing token from Clerk API."),s}}return{CLERK_FAPI:e?.frontendApiUrl||d(t)?.frontendApi,CLERK_TESTING_TOKEN:o}};var f="The Clerk Frontend API URL is required to bypass bot protection. Make sure the clerkSetup function is called during your global setup before setupClerkTestingToken is called.";var L=async({signInParams:e,windowObject:i})=>{try{let t=i||window;if(!t.Clerk.client)return;let r=t.Clerk.client.signIn;if(e.strategy==="password"){let o=await r.create(e);await t.Clerk.setActive({session:o.createdSessionId})}else{if(e.strategy==="phone_code"&&!e.identifier.includes("+155555501"))throw new Error(`Phone number should be a test phone number.
Example: +15555550100.
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:o}=await r.create({identifier:e.identifier}),s=e.strategy==="phone_code"?n=>n.strategy==="phone_code":n=>n.strategy==="email_code",a=o?.find(s);if(a){let n=e.strategy==="phone_code"?{strategy:e.strategy,phoneNumberId:a.phoneNumberId}:{strategy:e.strategy,emailAddressId:a.emailAddressId};await r.prepareFirstFactor(n);let c=await r.attemptFirstFactor({strategy:e.strategy,code:"424242"});if(c.status==="complete")await t.Clerk.setActive({session:c.createdSessionId});else throw new Error(`Status is ${c.status}`)}else throw new Error(`${e.strategy} is not enabled.`)}}catch(t){throw new Error(`Clerk: Failed to sign in: ${t?.message}`)}};export{_ as a,m as b,f as c,L as d};
//# sourceMappingURL=chunk-3CTIIA3F.mjs.map