cypress-testing-library icon indicating copy to clipboard operation
cypress-testing-library copied to clipboard

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

Open nickbreaton opened this issue 2 years ago • 6 comments

  • 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 avatar Feb 09 '23 23:02 nickbreaton

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

nickbreaton avatar Feb 09 '23 23:02 nickbreaton

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)
})

nickbreaton avatar Feb 10 '23 15:02 nickbreaton

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

Etzix avatar Mar 09 '23 14:03 Etzix

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

alex1701c avatar Mar 29 '23 08:03 alex1701c

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

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

jemilox avatar May 11 '23 16:05 jemilox

Any plans to release a fix for this? Thanks!

dannyskoog avatar Sep 03 '23 09:09 dannyskoog