Skip to content

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

Closed
mezei opened this issue Oct 28, 2020 · 9 comments
Labels
question Further information is requested

Comments

@mezei
Copy link

mezei commented Oct 28, 2020

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?

@eps1lon
Copy link
Member

eps1lon commented 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
Copy link
Author

mezei commented 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
Copy link
Member

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.

@nickserv nickserv added the question Further information is requested label Oct 29, 2020
@shengnoyi
Copy link

shengnoyi commented May 8, 2021

For those using react-app-rewired:

My project does not use a .babelrc before and i just want to use the same config from cra while running test.
The config in react scripts:

// 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 config-overrides.js like this:

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.

@yareyaredesuyo
Copy link

Support for the experimental syntax 'flow' isn't currently enabled

typescriptやで

@MatanBobi
Copy link
Member

@yareyaredesuyo can you share your .babelrc so we'll be able to see? My guess is that you're not adding @babel/preset-typescript

@ZennerBlade
Copy link

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?

@gugol2
Copy link

gugol2 commented Aug 31, 2022

Any solution for a Create React App without rewiring or ejecting?

@nickserv
Copy link
Member

nickserv commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

8 participants