testing-library / cypress-testing-library

🐅 Simple and complete custom Cypress commands and utilities that encourage good testing practices.
http://npm.im/@testing-library/cypress
MIT License
1.8k stars 152 forks source link

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

Open nickbreaton opened 1 year ago

nickbreaton commented 1 year ago

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 commented 1 year ago

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

nickbreaton commented 1 year ago

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 commented 1 year ago

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

alex1701c commented 1 year ago

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

jemilox commented 1 year ago
beforeEach(() => {
    cy.then(() => null)
})

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

dannyskoog commented 11 months ago

Any plans to release a fix for this? Thanks!