Better integration with e2e tests (ex. Cypress) using Password Grant flow
See original GitHub issueDescribe the problem you’d like to have solved
Our Cypress test suite, which currently follows the actual Auth0 login flow, is becoming flaky and unreliable.
I came across this official article, which suggests getting a token directly from the Auth0 API instead of automating the login flow: https://auth0.com/blog/end-to-end-testing-with-cypress-and-auth0/
However, the article assumes a traditional web app architecture, not a Single Page App, and there’s no clear way to pass an auth token to the Auth0 React SDK.
Alternatives and current work-arounds
Still working on finding a reliable workaround. I can mock the requests auth0-react makes to the Auth0 API, and intercept my own responses, but this requires knowledge of this library’s implementation details. I could also try to insert the token into localStorage using the cacheLocation="localStorage"
prop on Auth0Provider
, but again, that requires knowledge of implementation details.
Finally, I could try to abstract uses of the Auth0 React SDK (particularly isAuthenticated
, getTokenSilently
, and withAuthenticationRequired
) so that they aren’t ever called while using Cypress, but this would require a lot of refactoring, and nullify a lot of the convenience of this library.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Hey @dallonf - the existing sample/solution is bit of a band aid at the moment. We’re planning to do some work to make end to end testing more of a first class / officially supported citizen of the login SDKs, but that’s in it’s early stages.
The localStorage key is not a public api unfortuntaley - hence the work to come up with a better E2E testing experience for developers.
With the password grant? There’s no reason to run this request on every test, as long as your access token expiry lasts longer than your test run you could do this bit once in a
before
Here’s an additional solution for anyone who finds this: create a separate user for each test. As Cypress tests they should not be as granular as unit tests, so if you’re following that best practice this will not be as much work as it seems.
I also had the requirement that the tests be able to run in parallel, so the solution to login in
before
and then run the tests in sequence would not work.