UNPKG

@clerk/testing

Version:

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

1 lines 6.75 kB
{"version":3,"sources":["../../src/playwright/setup.ts","../../src/playwright/helpers.ts"],"sourcesContent":["import type { ClerkSetupOptions } from '../common';\nimport { fetchEnvVars } from '../common';\n\n/**\n * Sets up Clerk for testing by fetching the testing token from the Clerk Backend API.\n *\n * @param options.publishableKey - The publishable key for your Clerk dev instance.\n * @param options.frontendApiUrl - The frontend API URL for your Clerk dev instance, without the protocol. It overrides the Frontend API URL parsed from the publishable key.\n * @param options.debug - Enable debug logs.\n * @returns A promise that resolves when Clerk is set up.\n *\n * @throws An error if the publishable key or the secret key is not provided.\n * @throws An error if the secret key is from a production instance.\n * @throws An error if the testing token cannot be fetched from the Clerk Backend API.\n */\nexport const clerkSetup = async (options?: ClerkSetupOptions) => {\n const { CLERK_FAPI, CLERK_TESTING_TOKEN } = await fetchEnvVars(options);\n process.env.CLERK_FAPI = CLERK_FAPI;\n process.env.CLERK_TESTING_TOKEN = CLERK_TESTING_TOKEN;\n};\n","import type { Clerk, SignOutOptions } from '@clerk/types';\nimport type { Page } from '@playwright/test';\n\nimport type { ClerkSignInParams, SetupClerkTestingTokenOptions } from '../common';\nimport { signInHelper } from '../common';\nimport { setupClerkTestingToken } from './setupClerkTestingToken';\n\ndeclare global {\n interface Window {\n Clerk: Clerk;\n }\n}\n\ntype PlaywrightClerkLoadedParams = {\n page: Page;\n};\n\ntype ClerkHelperParams = {\n /**\n * Signs in a user using Clerk. This helper supports only password, phone_code and email_code first factor strategies.\n * Multi-factor is not supported.\n * This helper is using the `setupClerkTestingToken` internally.\n * It is required to call `page.goto` before calling this helper, and navigate to a not protected page that loads Clerk.\n *\n * If the strategy is password, the helper will sign in the user using the provided password and identifier.\n * If the strategy is phone_code, you are required to have a user with a test phone number as an identifier (e.g. +15555550100).\n * If the strategy is email_code, you are required to have a user with a test email as an identifier (e.g. your_email+clerk_test@example.com).\n *\n * @param opts.signInParams.strategy - The sign in strategy. Supported strategies are 'password', 'phone_code' and 'email_code'.\n * @param opts.signInParams.identifier - The user's identifier. Could be a username, a phone number or an email.\n * @param opts.signInParams.password - The user's password. Required only if the strategy is 'password'.\n * @param opts.page - The Playwright page object.\n * @param opts.setupClerkTestingTokenOptions - The options for the `setupClerkTestingToken` function. Optional.\n *\n * @example\n * import { clerk } from \"@clerk/testing/playwright\";\n *\n * test(\"sign in\", async ({ page }) => {\n * await page.goto(\"/\");\n * await clerk.signIn({\n * page,\n * signInParams: { strategy: 'phone_code', identifier: '+15555550100' },\n * });\n * await page.goto(\"/protected\");\n * });\n */\n signIn: (opts: PlaywrightClerkSignInParams) => Promise<void>;\n /**\n * Signs out the current user using Clerk.\n * It is required to call `page.goto` before calling this helper, and navigate to a page that loads Clerk.\n * @param opts.signOutOptions - A SignOutOptions object.\n * @param opts.page - The Playwright page object.\n *\n * @example\n * import { clerk } from \"@clerk/testing/playwright\";\n *\n * test(\"sign out\", async ({ page }) => {\n * await page.goto(\"/\");\n * await clerk.signIn({\n * page,\n * signInParams: { strategy: 'phone_code', identifier: '+15555550100' },\n * });\n * await page.goto(\"/protected\");\n * await clerk.signOut({ page });\n * await page.goto(\"/protected\");\n * // should redirect to sign in page\n * });\n */\n signOut: (opts: PlaywrightClerkSignOutParams) => Promise<void>;\n /**\n * Asserts that Clerk has been loaded.\n * It is required to call `page.goto` before calling this helper, and navigate to a page that loads Clerk.\n *\n * @param opts.page - The Playwright page object.\n */\n loaded: (opts: PlaywrightClerkLoadedParams) => Promise<void>;\n};\n\nconst loaded = async ({ page }: PlaywrightClerkLoadedParams) => {\n await page.waitForFunction(() => window.Clerk !== undefined);\n await page.waitForFunction(() => window.Clerk.loaded);\n};\n\ntype PlaywrightClerkSignInParams = {\n page: Page;\n signInParams: ClerkSignInParams;\n setupClerkTestingTokenOptions?: SetupClerkTestingTokenOptions;\n};\n\nconst signIn = async ({ page, signInParams, setupClerkTestingTokenOptions }: PlaywrightClerkSignInParams) => {\n const context = page.context();\n if (!context) {\n throw new Error('Page context is not available. Make sure the page is properly initialized.');\n }\n\n await setupClerkTestingToken({ context, options: setupClerkTestingTokenOptions });\n await loaded({ page });\n\n await page.evaluate(signInHelper, { signInParams });\n};\n\ntype PlaywrightClerkSignOutParams = {\n page: Page;\n signOutOptions?: SignOutOptions;\n};\n\nconst signOut = async ({ page, signOutOptions }: PlaywrightClerkSignOutParams) => {\n await loaded({ page });\n\n await page.evaluate(async options => {\n await window.Clerk.signOut(options);\n }, signOutOptions);\n};\n\nexport const clerk: ClerkHelperParams = {\n signIn,\n signOut,\n loaded,\n};\n"],"mappings":"oHAeO,IAAMA,EAAa,MAAOC,GAAgC,CAC/D,GAAM,CAAE,WAAAC,EAAY,oBAAAC,CAAoB,EAAI,MAAMC,EAAaH,CAAO,EACtE,QAAQ,IAAI,WAAaC,EACzB,QAAQ,IAAI,oBAAsBC,CACpC,EC2DA,IAAME,EAAS,MAAO,CAAE,KAAAC,CAAK,IAAmC,CAC9D,MAAMA,EAAK,gBAAgB,IAAM,OAAO,QAAU,MAAS,EAC3D,MAAMA,EAAK,gBAAgB,IAAM,OAAO,MAAM,MAAM,CACtD,EAQMC,EAAS,MAAO,CAAE,KAAAD,EAAM,aAAAE,EAAc,8BAAAC,CAA8B,IAAmC,CAC3G,IAAMC,EAAUJ,EAAK,QAAQ,EAC7B,GAAI,CAACI,EACH,MAAM,IAAI,MAAM,4EAA4E,EAG9F,MAAMC,EAAuB,CAAE,QAAAD,EAAS,QAASD,CAA8B,CAAC,EAChF,MAAMJ,EAAO,CAAE,KAAAC,CAAK,CAAC,EAErB,MAAMA,EAAK,SAASM,EAAc,CAAE,aAAAJ,CAAa,CAAC,CACpD,EAOMK,EAAU,MAAO,CAAE,KAAAP,EAAM,eAAAQ,CAAe,IAAoC,CAChF,MAAMT,EAAO,CAAE,KAAAC,CAAK,CAAC,EAErB,MAAMA,EAAK,SAAS,MAAMS,GAAW,CACnC,MAAM,OAAO,MAAM,QAAQA,CAAO,CACpC,EAAGD,CAAc,CACnB,EAEaE,EAA2B,CACtC,OAAAT,EACA,QAAAM,EACA,OAAAR,CACF","names":["clerkSetup","options","CLERK_FAPI","CLERK_TESTING_TOKEN","fetchEnvVars","loaded","page","signIn","signInParams","setupClerkTestingTokenOptions","context","setupClerkTestingToken","signInHelper","signOut","signOutOptions","options","clerk"]}