question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[@cypress/react] React 17 support for support configuration

See original GitHub issue

Current behavior

I am adding cypress and @cypress/react to an existing project which uses React 17. It works as expected when using the using-babel-typescript template and changing '@babel/preset-react' so be ['@babel/preset-react': {runtime: 'automatic'}] to make use of React 17’s _jsx automatic import fanciness, which the project uses for all its components already. However, when I try and create a centralised cy.mount helper along the lines of:

import { mount } from '@cypress/react';
import { ReactElement } from 'react';
import Wrapper from '/path/to/Wrapper';

Cypress.Commands.add('mount', (component) => {
  return mount(<Wrapper>{component}</Wrapper>);
});

I get the following error in Cypress:

The following error originated from your application code, not from Cypress.

  > Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

After playing with various different scenarios this seems to be because support files are bundled separately without the custom plugin for bundling components, so it is referencing the usual “classic” react import instead of the new _jsx version.

Desired behavior

@cypress/react” has a peerDependency on React 16, so I guess the ideal scenario is 1st class React 17 support and the ability to use runtime: 'automatic' for support bundling.

Test code to reproduce

Repro configs provided in the Current behavior section.

Versions

@cypress/react: 4.16.4 cypress: 6.8.0 @cypress/webpack-dev-server: 1.1.0 react: 17.0.1 react-dom: 17.0.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewLeedhamcommented, Apr 12, 2021

Nice got cy.mount working with v7. Thanks for the help!

1reaction
lmiller1990commented, Mar 29, 2021

Yep, reproduction works. I am digging into this… I suspect your suspicions around the support file are correct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Quickstart - Cypress Documentation
This tutorial will walk you through creating a React app and using Cypress ... any of the other supported frameworks in your project,...
Read more >
@cypress/react - npm
Test React components using Cypress. Latest version: 7.0.2, last published: 24 days ago. Start using @cypress/react in your project by ...
Read more >
Testing a React Application Setting up Cypress
Testing a modern React application. In this part of the series, we go over how to set up Cypress with React and launching...
Read more >
Testing React Apps In 2022 With Cypress: An In-Depth Guide ...
Using Chrome Dev Tools; Setting Breakpoints; Inpsecting Variables ... And this doesn't support the Cypress commands like contains() .
Read more >
Setting up Cypress in a React + Typescript project - YouTube
Hi everyone! Today we start working on setting up Cypress in our React + Typescript project, so that we can effectively create E2E...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found