cypress-react-selector icon indicating copy to clipboard operation
cypress-react-selector copied to clipboard

NextJS 10 Multiple random issues "Could not find instance of React in given element"

Open FDiskas opened this issue 4 years ago • 12 comments

[email protected]
[email protected]
[email protected]
node: v14.15.1

Create simple nextjs app by yarn create next-app --example with-typescript . create a test case.

Could not find instance of React in given element will be thrown at random intervals

describe('App tests', () => {
  before('Visits the homepage', () => {
    cy.visit('/');
    cy.viewport(800, 600);
    cy.waitForReact();
  });

  it('Locate Bob', () => {
    cy.react('Link').should('contain', 'Users List');
    cy.react('Link').contains('Users List').click();

    cy.react('Link').should('contain', '102: Bob');
    cy.react('Link').contains('102: Bob').click();

    cy.react('ListDetail').should('contain', 'Detail for Bob');
    cy.getReact('ListDetail')
      .getProps('item')
      .should('eql', { id: 102, name: 'Bob' });
  });
});

FDiskas avatar Dec 04 '20 19:12 FDiskas

hi @FDiskas , thanks for using the library. Have you tried defining the configuration as stated here?

abhinaba-ghosh avatar Dec 04 '20 20:12 abhinaba-ghosh

Sure. I used env settings

{
  "env": {
    "cypress-react-selector": {
      "root": "#root"
    }
  }
}

FDiskas avatar Dec 05 '20 11:12 FDiskas

Hi @FDiskas , it is possible that the react root css selector is not #root. Can you check if this is something else?

abhinaba-ghosh avatar Dec 16 '20 09:12 abhinaba-ghosh

I was testing on this repo https://github.com/FDiskas/devtalks-e2e/tree/cypress Nexjs root is __next and im sure that it matched setting in my test

FDiskas avatar Dec 16 '20 09:12 FDiskas

Thanks @FDiskas for sharing the repo. I will take a look.

abhinaba-ghosh avatar Dec 23 '20 06:12 abhinaba-ghosh

Hi, is there any update on this issue? Our project also use next.js and cypress. Error says Component not found <MyComponent />

suncihai avatar Oct 09 '21 02:10 suncihai

I also had a similar issue in nextjs. For me, what solved it, was to perform an ordinary get on one of the parent elements of the component, then applying the react method.

For example, I had a modal popup that had a data-test id applied to it. I would then alias this element, then use it to fetch the react component: cy.get(`[data-test="modal-popup"]`).as("modal"); cy.get("@modal").react("TargetComponent");

Can anyone confirm if this works on their end?

Also, setting or not setting the root env variable had not effect on the runnig of the test (perhaps connected to #236)

jericirenej avatar Oct 27 '21 12:10 jericirenej

Hi all, grant my apologies for not getting actively involved in the NextJs support for this library. I am having real busy weeks with Postman for some critical releases. I will not be able to take this task for another month. I would really appreciate if I get support in this. PRs are always welcome. Thanks again for the love! Cheers!

abhinaba-ghosh avatar Oct 27 '21 13:10 abhinaba-ghosh

I played around a bit. I found issue #251. I also found that a minified build had no problems finding a React instance, but an unminified build did. Is Next.js doing something odd?

resq looks for keys/properties on the React root HTML element, either _reactRootContainer or something beginning with _reactInternalInstance or _reactFiber. Stepping through in a debugger, the HTML element has no iterable keys. 🤷‍♂️

laurence-myers avatar Nov 02 '21 02:11 laurence-myers

Hi @bkirove, as this PR is merged and a new version of resq is released, can you give it a try for this problem? If all good, can release a version of cypress-react-selector.

abhinaba-ghosh avatar Jan 09 '22 09:01 abhinaba-ghosh

I updated the test project https://github.com/FDiskas/devtalks-e2e/tree/cypress with all dependencies as the newest Still didn't manage to work it properly - the same error

FDiskas avatar Oct 09 '23 18:10 FDiskas