-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Can't get it to work with the new JSX transform in React 17 & create-react-app v4 #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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 |
Here is a test repo with a simplified config: 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 |
This is an issue with your Babel configuration, not React Testing Library. The |
For those using react-app-rewired: My project does not use a // node_modules/react-scripts/config/jest/babelTransform.js
module.exports = babelJest.createTransformer({
presets: [
[
require.resolve('babel-preset-react-app'),
{
runtime: hasJsxRuntime ? 'automatic' : 'classic',
},
],
],
babelrc: false,
configFile: false,
}); The config in react-app-rewired: // node_modules/react-app-rewired/scripts/utils/babelTransform.js
const babelJest = require('babel-jest');
module.exports = babelJest.createTransformer({
presets: [require.resolve('babel-preset-react-app')],
plugins: [],
babelrc: true
}); So i override the const path = required("path")
module.exports = {
jest: function(config) {
if (config["transform"]["^.+\\.(js|jsx|mjs|cjs|ts|tsx)$"]) {
config["transform"]["^.+\\.(js|jsx|mjs|cjs|ts|tsx)$"] = path.resolve(
"node_modules/react-scripts/config/jest/babelTransform.js"
);
}
return config;
},
}; And it works perfectly. |
typescriptやで |
@yareyaredesuyo can you share your |
Facing the same issue As suggested above, my .babelrc already has runtime set to automatic
And my webpack has babel-loader without any additional options
I'm still getting react is not defined issue in my tests while the components are working perfectly Did anyone figure out the solution? |
Any solution for a Create React App without rewiring or ejecting? |
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. |
With the new JSX transform introduced in React (see: https://github.com/facebook/create-react-app/blob/master/CHANGELOG.md) it is no longer necessary to import React at the top of each component - as it should be in scope "by default" when Webpack is correctly configured.
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?
The text was updated successfully, but these errors were encountered: