1

单元测试时 Webpack 编译错误。我尝试修改测试文件还是这样,应该是webpack配置问题,测试报错组件错误

单元测试时 Webpack 编译错误。我尝试修改测试文件还是这样,应该是webpack配置问题,测试报错组件error webpack.test.config.js

const path = require('path');    
const config = {
  entry: {
    vendor: ["babel-polyfill", "react", "react-dom"],
    app: './index.js'
  },
  resolve: {
    alias: {
      'fonts': path.resolve(__dirname, 'public/fonts/'),
      '@components': path.resolve(__dirname, 'src/components/'),
      '@services': path.resolve(__dirname, 'src/services/'),
      '@routes': path.resolve(__dirname, 'src/routes/'),
      '@utils': path.resolve(__dirname, 'src/utils/'),
      '@less': path.resolve(__dirname, 'src/less/'),
      '@images': path.resolve(__dirname, 'public/images/'),
      '@public': path.resolve(__dirname, 'public/'),
    }
  },
  module: {
    rules: [{
      test: /\.js$/,
      exclude: /node_modules/,
      loader: 'babel-loader',
      query: {
        presets: ['react', 'es2015', 'stage-0']
      }
    }, {
      test: /\.less$/,
      exclude: /node_modules/,
      loader: 'style-loader!css-loader!less-loader'
    }]
  }
};

module.exports = config;

代理.spec.js

import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import Agent from '@components/agent/index';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

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

describe('<Agent />', () => {
  it('Agent renderning', function () {
    let app = shallow(<Agent/>);
    expect(app.find('button').exists());
  });
});

错误

 WEBPACK  Failed to compile with 1 error(s)

Error in ./test/unit/specs/Agent.spec.js

   TypeError: Cannot read property 'babel' of undefined

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ unit: `mocha-webpack --recursive test/unit/specs/*.js --webpack-config webpack.test.config.js`
npm ERR! Exit status 1
4

0 回答 0