How to login to Auth0 in a Cypress E2E test?

Hello.

I have a Sveltekit application that uses loginWithPopup() method from @auth0/auth0-spa-js. The cacheLocation is set to localstorage.

I am setting up E2E test for the application that tests situations with and without an Auth0 user logged in.

Currently, I am trying to use the Auth0 API endpoint GET /authorize, which redirects me to an Auth0 login form. After I fill in this form and submit my credentials, I am redirected to my provided redirect_uri with the access_token, token_type and expires_in as the URL slugs (https://127.0.0.1:5173/#access_token=ey...&expires_in=7200&token_type=Bearer), but I do not get any data added to my browser’s local storage. At this point, if I click the button that uses the loginWithPopup() method from @auth0/auth0-spa-js, my browser will redirect to my provided redirect_uri again, but this time with no slugs and with the Auth0 user data present in the local storage. Can the data be inserted into the local storage during the first redirect?

I am trying to set up E2E tests, so I am attempting to do the above with Cypress. I use Cypress to stub the loginWithPopup() method using cy.stub() to do nothing before it performs the process above. However, I cannot get the data into local storage when performing the above with Cypress. Therefore, my application never recognizes that an Auth0 user has logged in during the test.

I have even tried to use window.localstorage.setItem() in the test to insert the data into local storage, but it did not work.

Am I doing something wrong? How should I approach testing such a thing? What is the best way to log in as an Auth0 user in the test?

Thank you for your time.

2 Likes

have you solved? I have similar issue, looks token is not persisted so login form always is shown