question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better integration with e2e tests (ex. Cypress) using Password Grant flow

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
adamjmcgrathcommented, Mar 10, 2021

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 and value don’t appear to be officially documented.

The localStorage key is not a public api unfortuntaley - hence the work to come up with a better E2E testing experience for developers.

We’ve also seen some issues with rate limiting on automated tests; do you have suggestions to work around or prevent that?

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

0reactions
divinentdcommented, Mar 17, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

End-to-End Testing with Cypress and Auth0
Click on the Grant Types tab, and check the Password grant. ... Cypress is a modern front-end testing tool that makes writing integration...
Read more >
Auth0 Authentication | Cypress Documentation
This guide is setup for testing against an Auth0 Single Page Application using the ... select "Grant Types" tab and check "Password" (unchecked...
Read more >
OAuth and E2E Testing: How to Ensure User Authentication ...
OAuth and E2E Testing : How to Ensure User Authentication Using Cypress - Taylor Ellsworth. Watch later. Share. Copy link.
Read more >
How to test for accessibility with Cypress - Deque Systems
Cypress is a complete end-to-end testing tool. It reduces complexity by offering an all-inclusive testing platform, rather than requiring you to ...
Read more >
@synthetixio/synpress - npm
It also provides an easy way to use and access metamask straight from your e2e tests with all features of cypress and playwright....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found