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.

logged-in state is lost after test causes another POST request

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
jennifer-shehanecommented, Apr 29, 2020

@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-cookies

1reaction
jeanlescurecommented, Jan 9, 2020

Same error on a React application

Read more comments on GitHub >

github_iconTop Results From Across the Web

Requests can only be made in the LoggedIn state, not the ...
A few issues: 1) var Request = require('tedious').Request; seems to be missing; 2) the insert statement specifies six columns, but only five ...
Read more >
Understanding Axios POST requests - LogRocket Blog
Learn how to use the Axios POST method in both vanilla JavaScript and in a framework like React to send requests.
Read more >
HTTP Tests - Laravel - The PHP Framework For Web Artisans
In general, each of your tests should only make one request to your application. Unexpected behavior may occur if multiple requests are executed...
Read more >
Troubleshooting requests - Postman Learning Center
If Postman fails to send your request, you may be experiencing connectivity issues. Check your connection by attempting to open a page in...
Read more >
REST API Testing Strategy: What Exactly Should You Test?
For each API request, the test would need to take the following actions: 1. Verify correct HTTP ... 201 for POST or PUT...
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