reactjs - Babel throwing 对实验语法 'jsx' 的支持当前未启用

标签 reactjs jestjs enzyme babeljs

我开始使用 Jest 和 Enzyme 为 React 应用程序新编写单元测试用例,并尝试使用像 "test": "jest --watch" 这样的 Jest 运行测试用例时而是 "test": "react-scripts test"测试通过 babel 为运行者理解 react 语法。
并且一直在使用 babel 一步一步地进行设置,但是这个错误 Support for the experimental syntax 'jsx' isn't currently enabled阻止我走得更远。正如一些线程中所建议的,我一直在尝试使用 npm install --save-dev @babel/plugin-transform-react-jsx并尝试将相同的插件添加到 babel 配置中,如下面的 package.json 文件所示,但仍然没有运气。
enter image description here
这是我的 package.json

{
  "name": "multitheme-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^3.9.0",
    "@material-ui/icons": "^3.0.2",
    "axios": "^0.18.0",
    "babel-plugin-transform-export-extensions": "^6.22.0",
    "jest-css-modules": "^2.1.0",
    "react": "^16.8.1",
    "react-dom": "^16.8.1",
    "react-redux": "^6.0.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.5",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "babel": {
    "presets": [
      "@babel/preset-react",
      "@babel/preset-env",
      "@babel/preset-flow"
    ],
    "plugins": [
      "@babel/plugin-transform-modules-commonjs",
      "@babel/plugin-transform-react-jsx"
      
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.10.4",
    "@babel/core": "^7.10.4",
    "@babel/plugin-proposal-class-properties": "^7.10.4",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-modules-commonjs": "^7.10.4",
    "@babel/plugin-transform-react-jsx": "^7.10.4",
    "@babel/preset-env": "^7.10.4",
    "@babel/preset-es2015": "^7.0.0-beta.53",
    "@babel/preset-flow": "^7.10.4",
    "@babel/preset-react": "^7.10.4",
    "@babel/runtime": "^7.10.4",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^26.1.0",
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-decorators-legacy": "^1.3.5",
    "babel-preset-stage-0": "^6.24.1",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2",
    "enzyme-to-json": "^3.5.0",
    "jest": "^23.6.0",
    "jest-cli": "^26.1.0"
  },
  "jest": {
    "verbose": true,
    "clearMocks": true,
    "collectCoverage": true,
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupTest.js",
    "transform": {
      "^.+\\.js$": "babel-jest"
    },
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(css|scss)$": "identity-obj-proxy"
    }
  }
}
这是我的测试用例文件
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { shallow, configure } from 'enzyme';

import App from './App';

configure({adapter: new Adapter()});


describe('MyComponent', () => {
  it('should render correctly in "debug" mode', () => {
    const component = shallow(<App debug />);
    expect(component).toMatchSnapshot();
  });
});

最佳答案

查看错误,它看起来无法加载预设以进行 react 。切换到配置文件并将 babel 的配置从 package.json 移动到它。
一个示例文件如下所示,将位于与 package.json 相同的级别并称为 babel.config.js

module.exports = function (api) {
  const presets = [
      '@babel/preset-env',
    '@babel/preset-react',
    '@babel/preset-flow'
  ];
  const plugins = [
    '@babel/plugin-transform-runtime',
  ];

  /** this is just for minimal working purposes,
     * for testing larger applications it is
     * advisable to cache the transpiled modules in
     * node_modules/.bin/.cache/@babel/register* */
  api.cache(false);

  return {
    presets,
    plugins
  };
};

关于reactjs - Babel throwing 对实验语法 'jsx' 的支持当前未启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62820035/

相关文章:

reactjs - 期望在 Jest 中包含一组值中的文本,带有 React 的 enzyme

javascript - 为 Node CommonJS 模块创建不可变类

reactjs - React 模式对话框的内容不可用于使用 mount() 的 enzyme 测试

reactjs - 无法使用react-router v4 GET "/About"(生产帮助)

ios - 运行 react native ios 应用程序时找不到文件依赖项错误

javascript - 如何在 React Native 中对过滤器函数进行单元测试?

reactjs - JEST STYLED COMPONENTS - toHaveStyleRule 没有找到属性

javascript - 我正在尝试测试一个将另一个函数作为参数的函数

javascript - react native : How to make modular components using local image and data assets?

javascript - 创建一个函数来排序表 React,JSX