logged-in state is lost after test causes another POST request
See original GitHub issueI’m confused here. I have this very simple test that logs my user in, then visits a page that requires the user to be logged in, views a form and clicks the submit button
describe('Logs In and Submits Form', () => {
it('Mocks Login', () => {
cy.mockLoginAndGoto('form');
});
it('Sees Form', () => {
cy.get('form', {
timeout: 5000,
}).should('be.visible');
});
it('Clicks Submit Button', () => {
cy.get('[data-cy="submit-button"]').click();
});
});
The login part uses JWT, anyway that part works fine. It then visits the route /form
and views the form element. This form is only visible to logged-in users, I can clearly see that at this point the user is logged in, I can see that from the UI showing the user’s name. So far so good. Then in the last step, a button is clicked which triggers a POST
requests, which passes the authorization token retrieved from the first step.
This part fails because for some reason I can’t figure out, by the time it reaches the 3rd step, the user is no longer logged in, and as such the authorization token is not included with the POST
request.
This “flow” works fine if you run it manually.
Now, if I run all 3 steps as a single it()
, then it works correctly
describe('Logs In and Submits Form', () => {
it('Mocks Login, sees form, clicks submit button', () => {
cy.mockLoginAndGoto('form');
cy.get('form', {
timeout: 5000,
}).should('be.visible');
cy.get('[data-cy="submit-button"]').click();
});
});
This works correctly. How come? What am I missing?
This is a simplified version of the real test. The real test involves logging in, testing a bunch of steps then at the end clicking the submit button and submitting a form that’s been filled out by the previous steps. I don’t want to have to mock the login before each of those steps. I also don’t want to have to eliminate all my steps and run 20 sets of directives as a single it()
.
Why is it that all steps keep my user logged in but when I reach the step that causes a POST
request, only then does it fail and reverts my state to logged-out? But as a single it()
it works?
Thanks in advance for the help
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top GitHub Comments
@geevb cookies are not preserved across tests. If you set them in the
before
, they will be removed before the second test runs. See how to preserve cookies across tests: https://on.cypress.io/cookies#Set-global-default-cookiesSame error on a React application