Sharing Authentication State

Learn about sharing authentication states and how to use it to speed up the test.

We know that the state must not be shared between subsequent tests because it’s the first reason for brittle tests .

Remember the Testing Rules: why should we not share the state between tests? Imagine this scenario:

  • test a registers a new user and performs actions that require the user to be registered.
  • test b preforms actions that require the user to be registered

You are going to break test b test if

  • You run it alone with it.only().

  • You skip the test a with it.skip().

  • You change the order.

  • You move the test b to another file or vice-versa.

Every test must be independent.

In the previous lesson, we improved test performance, but it’s still quite slow. To maintain independence, we could write a custom command that registers a new user, stores the user token, and restores the token before the next step!

One thing at a time:

  1. The RealWorld front-end stores the user token with a jwt local storage field.

  2. We can copy it after the first registration (it can be accessed in Cypress with localStorage.jwt).

  3. Then, we can restore it for every future test (checking that the user is still valid and authenticated for the back-end).

Let’s write the cy.signupV3 custom command, starting from the cy.signupV2 command that’s as follows:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy