Babel-jest不会在node_modules中转换JSX

时间:2019-04-04 07:09:49

标签: reactjs jestjs babel react-testing-library

运行测试时,Babel-jest不会在node_modules文件夹中转换JSX / ES6 不使用node_modules中其他组件的React组件正确通过了所有测试。

由于https://github.com/facebook/jest/issues/6229,尝试添加transformIgnorePatterns并重命名为babel.config.js

package.json

"devDependencies": {
    "@babel/core": "7.1.2",
    "@babel/node": "7.0.0",
    "@babel/plugin-syntax-dynamic-import": "7.2.0",
    "@babel/plugin-transform-modules-commonjs": "^7.4.0",
    "@babel/preset-env": "7.2.0",
    "@babel/preset-react": "7.0.0",
    "babel-eslint": "10.0.1",
    "babel-jest": "24.5.0",
    "babel-loader": "8.0.4",
    "jest": "24.5.0",
    "jest-dom": "3.1.3",
    .......
  },

babel.config.js

module.exports = {
  presets: [
    '@babel/preset-env',
    '@babel/preset-react',
  ],
  plugins: [
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-proposal-class-properties',
    'react-hot-loader/babel',
  ],
  env: {
    test: {
      plugins: ['@babel/plugin-transform-modules-commonjs', '@babel/plugin-proposal-class-properties'],
    },
  },
};

jest.config.js

module.exports = {
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
  },
  moduleNameMapper: {
    '\\.(css|less|scss|sss)$': 'identity-obj-proxy',
  },
  testEnvironment: 'jsdom',
};

1 个答案:

答案 0 :(得分:0)

实际上,下面的链接对我有所帮助。 从transformIgnorePatternsnode_modules添加我的组件就足够了,因此babel-jest忽略所有文件而不是我的组件
image from jestjs docs
https://jestjs.io/docs/en/tutorial-react-native#transformignorepatterns-customization