You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't get it to work with the new JSX transform in React 17 & create-react-app v4 #810
However if I attempt to do the same with test files I get the usual "React is not defined" error.
React has to be imported both in the target component & the test file for it to actually work.
Am I missing some configuration for this library or is this just not supported yet?
I'm not sure yet whether this is a bug or I'm just missing some configuration, babel transform setting, etc for either package, as there is no documentation for this case in react-testing-library (because this stuff is still very new).
The issue is that even though I can exclude calling import React from 'react' for most of my components (due to the new JSX transform), I still need to call this import for components which have unit tests. else I get the above error.
This is an issue with your Babel configuration, not React Testing Library. The .babelrc (which is only used by Jest) is missing "runtime": "automatic" so the new JSX transform is disabled. You can simplify your Babel configs and fix this by merging both your Babel configs into .babelrc with this option and removing the options from babel-loader, which I've done in mezei/react-testing-lib-new-transform#1.
Reacted by Matan Borenkraout, Mezei, Sebastian "Sebbie" Silbermann, Christian Murphy, Chris Tybur, Kaspars Putniņš, Zoltan Gyarmati, Andrew Revinsky, David Lemayian ✨, kojidev and 3 more
Reacted by David Lemayian ✨, Subroto and Zain Fathoni
CRA is not actively maintained by Facebook currently. 10 PRs have been opened in the last month, none of them have been merged. I've had UX-critical PRs open for months without a response. Additionally, only 3 people have committed to origin this year.
Activity
eps1lon commentedon Oct 28, 2020
It doesn't look like this bug report has enough info for one of us to reproduce it.
Please provide a link to a repository on GitHub.
Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve
mezei commentedon Oct 28, 2020
Here is a test repo with a simplified config:
https://github.com/mezei/react-testing-lib-new-transform
I'm not sure yet whether this is a bug or I'm just missing some configuration, babel transform setting, etc for either package, as there is no documentation for this case in react-testing-library (because this stuff is still very new).
The issue is that even though I can exclude calling
import React from 'react'for most of my components (due to the new JSX transform), I still need to call this import for components which have unit tests. else I get the above error.nickserv commentedon Oct 29, 2020
This is an issue with your Babel configuration, not React Testing Library. The
.babelrc(which is only used by Jest) is missing"runtime": "automatic"so the new JSX transform is disabled. You can simplify your Babel configs and fix this by merging both your Babel configs into.babelrcwith this option and removing the options frombabel-loader, which I've done in mezei/react-testing-lib-new-transform#1.shengnoyi commentedon May 8, 2021
For those using react-app-rewired:
My project does not use a
.babelrcbefore and i just want to use the same config from cra while running test.The config in react scripts:
The config in react-app-rewired:
So i override the
config-overrides.jslike this:And it works perfectly.
yareyaredesuyo commentedon May 10, 2021
typescriptやで
MatanBobi commentedon May 10, 2021
@yareyaredesuyo can you share your
.babelrcso we'll be able to see? My guess is that you're not adding@babel/preset-typescriptZennerBlade commentedon May 18, 2021
Facing the same issue
As suggested above, my .babelrc already has runtime set to automatic
"presets": [ "@babel/preset-env", [ "@babel/preset-react", { "runtime": "automatic" } ] ],And my webpack has babel-loader without any additional options
{ test: /\.(js|jsx)$/, exclude: /node_modules/, use: ['babel-loader', 'eslint-loader'], },I'm still getting react is not defined issue in my tests while the components are working perfectly
Did anyone figure out the solution?
No need to add "import React" everywhere
gugol2 commentedon Aug 31, 2022
Any solution for a Create React App without rewiring or ejecting?
nickserv commentedon Aug 31, 2022
CRA is not actively maintained by Facebook currently. 10 PRs have been opened in the last month, none of them have been merged. I've had UX-critical PRs open for months without a response. Additionally, only 3 people have committed to origin this year.
I'd recommend switching to Vite with this template: https://github.com/nickmccurdy/vite-react-testing
Vite supports configuring pretty much anything without rewiring or ejecting too.