Webpack,React&摩卡测试:缺少类属性转换

时间:2016-09-12 18:58:29

标签: reactjs babel

我这个问题已经持续了一个多星期了。代码在浏览器中运行良好,我没有错误,但是当我运行mocha test时,我得到Missing class properties transform的错误。我通过npm安装它,甚至删除了我的软件包并重新安装它们但仍然没有。我在这里阅读了很多帖子(即Error: Missing class properties transform),我仍然想出了这个错误。我在这里错过了什么?谢谢你的帮助。

webpack.config.js:

    ...
    module: {
        loaders: [
          {
            test: /\.(js|jsx)$/,
            include: [
              path.resolve(__dirname, "public/app")
            ],
            exclude: /node_modules/,
            loaders: [
              'react-hot',
              'babel?presets[]=react,presets[]=es2015,presets[]=stage-0'
            ],
            loader: 'babel',
            query: {
              "plugins": ['transform-decorators-legacy']
            }
          }, 
          {
            test: /\.json?$/,
            loader: 'json'
          }, 
          {
            test: /\.css$/,
            loaders: ['style', 'css']
          },
          {
            test: /\.less$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader!less-loader')
          }
        ]
      }
      ...

.babelrc:

{
  "presets": ["react", "es2015", "stage-0"],
  "plugins": [
    "transform-class-properties"
  ]
}

的package.json:

...
"babel-loader": "^6.2.4",
"babel-plugin-react-transform": "^2.0.2",
"babel-plugin-transform-class-properties": "^6.11.5",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-survivejs-kanban": "^0.3.3",
"babel-relay-plugin-loader": "^0.9.0",
...
"css-loader": "^0.23.1",
...
"style-loader": "^0.13.1",
...

如果需要,我可以提供任何其他详细信息。如果看到整个代码库会有所帮助,你可以在这里看到: https://github.com/DanDeller/sputnik

1 个答案:

答案 0 :(得分:1)

Mocha没有看你的webpack.config,它是一个单独的系统。要告诉mocha通过babel加载你的测试,你需要标记它。

mocha test --require babel-core/register