E2E Testing an App with Clerk Authentication in Cypress

Background: Clerk is a hosted authentication and user management product.

I recently started writing E2E tests in Cypress for an app that uses Clerk for authentication, and there wasn’t anything out there to guide me, so here’s what I ended up with…


This content originally appeared on DEV Community and was authored by Lynn Romich

Background: Clerk is a hosted authentication and user management product.

I recently started writing E2E tests in Cypress for an app that uses Clerk for authentication, and there wasn’t anything out there to guide me, so here’s what I ended up with after fiddling with it for a bit.

(Note: In my case, this is a Next.js app using Clerk’s Next.js SDK, but my understanding is that this code will work everywhere, because their client SDKs all ultimately use ClerkJS under the hood.)

I wrote a custom Cypress command that waits for Clerk to load, signs the user out if they aren’t signed out already, and then signs in with test credentials (see here for how you can set these so that they’re accessible via Cypress.env()).

Cypress.Commands.add(`initializeAuth`, () => {
  cy.log(`Initializing auth state.`)

  cy.visit(`/`)

  cy.window()
    .should(window => {
      expect(window).to.not.have.property(`Clerk`, undefined)
      expect(window.Clerk.isReady()).to.eq(true)
    })
    .then(async window => {
      await window.Clerk.signOut()
      await window.Clerk.client.signIn.create({
        identifier: Cypress.env(`TEST_USER_IDENTIFIER`),
        password: Cypress.env(`TEST_USER_PASSWORD`),
      })
    })
})

If you’re using TypeScript to write your tests (which I recommend!), you’ll also want to add this command to your custom command types.

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Initialize auth to a state where you're logged in as the test user.
       * @example cy.initializeAuth()
       */
      initializeAuth(): Chainable<void>
    }
  }
}

Ultimately, you’ll probably want to use this command in a before or beforeEach hook to reset the auth state before every test, like so:

describe(`Test Page`, () => {
  beforeEach(() => {
    cy.resetDatabase() // another custom command
    cy.initializeAuth()
  })

  // ... tests go here
})

Happy testing! Please let me know if you run into problems with this approach.


This content originally appeared on DEV Community and was authored by Lynn Romich


Print Share Comment Cite Upload Translate Updates
APA

Lynn Romich | Sciencx (2022-04-21T00:39:25+00:00) E2E Testing an App with Clerk Authentication in Cypress. Retrieved from https://www.scien.cx/2022/04/21/e2e-testing-an-app-with-clerk-authentication-in-cypress/

MLA
" » E2E Testing an App with Clerk Authentication in Cypress." Lynn Romich | Sciencx - Thursday April 21, 2022, https://www.scien.cx/2022/04/21/e2e-testing-an-app-with-clerk-authentication-in-cypress/
HARVARD
Lynn Romich | Sciencx Thursday April 21, 2022 » E2E Testing an App with Clerk Authentication in Cypress., viewed ,<https://www.scien.cx/2022/04/21/e2e-testing-an-app-with-clerk-authentication-in-cypress/>
VANCOUVER
Lynn Romich | Sciencx - » E2E Testing an App with Clerk Authentication in Cypress. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/21/e2e-testing-an-app-with-clerk-authentication-in-cypress/
CHICAGO
" » E2E Testing an App with Clerk Authentication in Cypress." Lynn Romich | Sciencx - Accessed . https://www.scien.cx/2022/04/21/e2e-testing-an-app-with-clerk-authentication-in-cypress/
IEEE
" » E2E Testing an App with Clerk Authentication in Cypress." Lynn Romich | Sciencx [Online]. Available: https://www.scien.cx/2022/04/21/e2e-testing-an-app-with-clerk-authentication-in-cypress/. [Accessed: ]
rf:citation
» E2E Testing an App with Clerk Authentication in Cypress | Lynn Romich | Sciencx | https://www.scien.cx/2022/04/21/e2e-testing-an-app-with-clerk-authentication-in-cypress/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.