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

any find* command fails when is first to run in a test context #253

Open
nickbreaton opened this issue Feb 9, 2023 · 6 comments
Open

any find* command fails when is first to run in a test context #253

nickbreaton opened this issue Feb 9, 2023 · 6 comments

Comments

@nickbreaton
Copy link

nickbreaton commented Feb 9, 2023

  • cypress-testing-library version: 9.0.0
  • node version: 19.4.0
  • npm (or yarn) version: 9.2.0 (npm)

What you did:

I was migrating from an older version of testing library where we have a Cypress test set up with a describe option of { testIsolation: false }, a before block, two it blocks, with the second it block beginning with a find* command.

What happened:

When the second it block ran, the following error was produced:

  TypeError: Cannot read properties of undefined (reading 'get')
     at $Command.command (webpack:///./src/index.js:40:29)
  From previous event:
     at CommandQueue.runCommand (http://localhost:50133/__cypress/runner/cypress_runner.js:149951:8)
     at next (http://localhost:50133/__cypress/runner/cypress_runner.js:150151:19)

Reproduction repository:

I've opened forked this repo with a quick fix for the issue, including a reproduction via your test suite.

Problem description:

It appears in this very specific circumstance, this.get('prev') can yield undefined making the following .get fail.

Suggested solution:

Add a nullish check before accessing the second .get. See forked repo's commit.


Thanks for continuing to provide an amazing utility. 🙏

@nickbreaton
Copy link
Author

I'm happy to PR the fix I've made in my fork, including the tests if desired.

@nickbreaton nickbreaton changed the title any find* command fails when is the first to run in a test context any find* command fails when is first to run in a test context Feb 9, 2023
@nickbreaton
Copy link
Author

Also, in case others run into this before its resolved, setting the following globally seems to ensure this.get('prev') is always defined by the time your test runs.

beforeEach(() => {
    cy.then(() => null)
})

@Etzix
Copy link

Etzix commented Mar 9, 2023

I ran into this today. Thank you for finding and providing a solution 😄

@alex1701c
Copy link

I also ran into this issue. Thanks for the explanation & workaround! A MR would be appreciated :)

@jemilox
Copy link

jemilox commented May 11, 2023

beforeEach(() => {
    cy.then(() => null)
})

I put this in my e2e.ts file and it worked!

@dannyskoog
Copy link

Any plans to release a fix for this? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants