UNPKG

@clerk/testing

Version:

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

10 lines (6 loc) 5.09 kB
"use strict";var P=Object.create;var p=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty;var R=(e,r)=>{for(var t in r)p(e,t,{get:r[t],enumerable:!0})},C=(e,r,t,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of I(r))!K.call(e,n)&&n!==t&&p(e,n,{get:()=>r[n],enumerable:!(o=S(r,n))||o.enumerable});return e};var F=(e,r,t)=>(t=e!=null?P(L(e)):{},C(r||!e||!e.__esModule?p(t,"default",{value:e,enumerable:!0}):t,e)),v=e=>C(p({},"__esModule",{value:!0}),e);var A={};R(A,{clerk:()=>T,clerkSetup:()=>f,setupClerkTestingToken:()=>k});module.exports=v(A);var E="__clerk_testing_token";var u=require("@clerk/backend"),d=require("@clerk/shared/keys"),w=F(require("dotenv")),y=async e=>{let{debug:r=!1,dotenv:t=!0,...o}=e||{},n=a=>{r&&console.log(`Clerk: ${a}`)};n("Setting up Clerk..."),t&&w.default.config({path:[".env.local",".env"]});let c=o.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,i=o.secretKey||process.env.CLERK_SECRET_KEY,s=process.env.CLERK_TESTING_TOKEN;if(!c)throw new Error("You need to set the CLERK_PUBLISHABLE_KEY environment variable.");if(!i&&!s)throw new Error("You need to set the CLERK_SECRET_KEY or the CLERK_TESTING_TOKEN environment variable.");if(i&&!s){if((0,d.isProductionFromSecretKey)(i))throw new Error("You are using a secret key from a production instance, but Testing Tokens only work in development instances.");n("Fetching testing token from Clerk Backend API...");try{let a=o?.apiUrl||process.env.CLERK_API_URL;s=(await(0,u.createClerkClient)({secretKey:i,apiUrl:a}).testingTokens.createTestingToken()).token}catch(a){throw console.error("Failed to fetch testing token from Clerk API."),a}}return{CLERK_FAPI:e?.frontendApiUrl||(0,d.parsePublishableKey)(c)?.frontendApi,CLERK_TESTING_TOKEN:s}};var h="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 _=async({signInParams:e,windowObject:r})=>{try{let t=r||window;if(!t.Clerk.client)return;let o=t.Clerk.client.signIn;if(e.strategy==="password"){let n=await o.create(e);await t.Clerk.setActive({session:n.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:n}=await o.create({identifier:e.identifier}),c=e.strategy==="phone_code"?s=>s.strategy==="phone_code":s=>s.strategy==="email_code",i=n?.find(c);if(i){let s=e.strategy==="phone_code"?{strategy:e.strategy,phoneNumberId:i.phoneNumberId}:{strategy:e.strategy,emailAddressId:i.emailAddressId};await o.prepareFirstFactor(s);let a=await o.attemptFirstFactor({strategy:e.strategy,code:"424242"});if(a.status==="complete")await t.Clerk.setActive({session:a.createdSessionId});else throw new Error(`Status is ${a.status}`)}else throw new Error(`${e.strategy} is not enabled.`)}}catch(t){throw new Error(`Clerk: Failed to sign in: ${t?.message}`)}};var f=async e=>{let{CLERK_FAPI:r,CLERK_TESTING_TOKEN:t}=await y(e);process.env.CLERK_FAPI=r,process.env.CLERK_TESTING_TOKEN=t};var k=async({context:e,options:r,page:t})=>{let o=e??t?.context();if(!o)throw new Error("Either context or page must be provided to setup testing token");let n=r?.frontendApiUrl||process.env.CLERK_FAPI;if(!n)throw new Error(h);let c=`https://${n}/v1/**/*`;await o.route(c,async i=>{let s=new URL(i.request().url()),a=process.env.CLERK_TESTING_TOKEN;a&&s.searchParams.set(E,a);try{let m=await i.fetch({url:s.toString()}),l=await m.json();l?.response?.captcha_bypass===!1&&(l.response.captcha_bypass=!0),l?.client?.captcha_bypass===!1&&(l.client.captcha_bypass=!0),await i.fulfill({response:m,json:l})}catch{await i.continue({url:s.toString()}).catch(console.error)}})};var g=async({page:e})=>{await e.waitForFunction(()=>window.Clerk!==void 0),await e.waitForFunction(()=>window.Clerk.loaded)},O=async({page:e,signInParams:r,setupClerkTestingTokenOptions:t})=>{let o=e.context();if(!o)throw new Error("Page context is not available. Make sure the page is properly initialized.");await k({context:o,options:t}),await g({page:e}),await e.evaluate(_,{signInParams:r})},b=async({page:e,signOutOptions:r})=>{await g({page:e}),await e.evaluate(async t=>{await window.Clerk.signOut(t)},r)},T={signIn:O,signOut:b,loaded:g};0&&(module.exports={clerk,clerkSetup,setupClerkTestingToken}); //# sourceMappingURL=index.js.map