Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress session validation failure results in a 'existingSession.setup is not a function' error #17805

Closed
Lasidar opened this issue Aug 19, 2021 · 14 comments · Fixed by #21226
Closed
Assignees
Labels
topic: session Issues when using session command type: bug

Comments

@Lasidar
Copy link

Lasidar commented Aug 19, 2021

Current behavior

While using the new experimental feature sessions in cypress 8.2.0, adding a session validation routine does not seem to work correctly. Specifically, when a session exists, but the validation routine returns false, an error is thrown saying that the setup function does not exist.

Here is a sample block of code that can reproduce this issue:
Screenshot from 2021-08-19 09-53-14

When running this for the first time:
Screenshot from 2021-08-19 09-52-29

When re-running this test:
Screenshot from 2021-08-19 09-52-45

Desired behavior

Cypress should fail the validation, and re-run the setup function as specified in the docs. For some reason, the current code somehow loses the setup function when validation fails.

Test code to reproduce

  it('fails the session setup on the second run', () => {
    var validateFlag = false
    cy.session('test', () => {
      validateFlag = true
      cy.log('setup')
    },
    {
      validate() {
        cy.log('validate')
        if (validateFlag) {
          return true
        }
        else {
          return false
        }
      },
    })
  });

Cypress Version

8.2.0

Other

No response

@jennifer-shehane jennifer-shehane added the topic: session Issues when using session command label Aug 19, 2021
@kuceb
Copy link
Contributor

kuceb commented Aug 19, 2021

Thanks, the existingSession.setup error is definitely a bug. This might be an issue related to retries, if you turn off retries do you get the same error?

However the test should still fail, that is expected. In order to prevent an endless loop if your validate function always returns false, we fail the test if we have just initialized the session via the setup function.
https://docs.cypress.io/api/commands/session#The-command-log

Note that in cases where the validate function fails immediately after setup creates the session, the test will fail with an error.

We should improve the error message to explain this to the user

@kuceb kuceb self-assigned this Aug 19, 2021
@Lasidar
Copy link
Author

Lasidar commented Aug 19, 2021

@bkucera disabling retries does not seem to make a difference.

I understand having a validation continuously fail will ultimately cause the test to fail. I am more concerned about the setup command not being found.

@alexniculae
Copy link

Hello @bkucera - same as @Lasidar, disabling retries did not change anything for me either.

Same concern here, not a problem that this is failing, it's normal in the end - but the recreation process for a new session is never initiated because of the existingSession.setup is not a function error.

@alvaromartmart
Copy link

I can confirm this is an issue. It basically renders the validate function useless 😅 until this is fixed.

@bahmutov
Copy link
Contributor

Also hit this problem

@bahmutov
Copy link
Contributor

Few stack screenshots, seems the setup suddenly becomes a fn.toString() output. Is this serialized somewhere?

Screen Shot 2021-09-28 at 15 58 31

Screen Shot 2021-09-28 at 15 58 52

@bahmutov
Copy link
Contributor

Maybe in

.then(async () => {
await navigateAboutBlank()
const data = await sessions.getCurrentSessionData()
Cypress.log({ groupEnd: true, emitOnly: true })
_.extend(existingSession, data)

We might need to omit setup from serialized properties, otherwise when restored the string overwrites the original callback

@bahmutov
Copy link
Contributor

Ohh, it was a little bit lower in

// we have a saved session on the server AND setup matches
if (serverStoredSession && serverStoredSession.setup === existingSession.setup.toString()) {
_.extend(existingSession, serverStoredSession)
existingSession.hydrated = true

Patching it to be

if (serverStoredSession && serverStoredSession.setup === existingSession.setup.toString()) {

            lodash__WEBPACK_IMPORTED_MODULE_0___default.a.extend(existingSession,
              lodash__WEBPACK_IMPORTED_MODULE_0___default.a.omit(serverStoredSession, 'setup'));

fixed my problem

@alexniculae
Copy link

Hello @bkucera - is the change propose by Bahmutov, or another fix for this issue, planned to be introduce in an upcoming release?
Thank you,
Alex

@ComicScrip
Copy link

Had the same issue, any news on this ?

@eLarocque
Copy link

Same issue here. Providing the validate function is useless until this is fixed.
I know this is an experimental feature (and a great one at that!) but would love this issue prioritized :)
Thanks!

@DanaGoyette
Copy link

DanaGoyette commented Feb 16, 2022

In my case, since I haven't generally had sessions become invalid in the middle of the run, I can work around it by clearing all sessions at the start of the run. In support/index.ts (might need syntax tweaking if using js):

before(() => {
  /**
   * Workaround for https://github.com/cypress-io/cypress/issues/17805
   * @see https://docs.cypress.io/api/commands/session#Explicitly-clearing-sessions
   */
  Cypress.session.clearAllSavedSessions()
})

@cypress-bot cypress-bot bot added the stage: needs review The PR code is done & tested, needs review label Apr 27, 2022
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 28, 2022

The code for this is done in cypress-io/cypress#21226, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Apr 28, 2022
@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 9, 2022

Released in 9.6.1.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v9.6.1, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators May 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: session Issues when using session command type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants