1

当我用酶进行业力测试时,出现错误:

only one instance of babel-polyfill is allowed at ...

这是导致此错误的测试文件:

import React from 'react';
import { Provider } from 'react-redux';
import configureStore from '../../src/common/store/configureStore';
import { shallow,mount } from 'enzyme';
import ChatContainer from '../../src/common/containers/ChatContainer';


const initialState = window.__INITIAL_STATE__;
const store = configureStore(initialState);




describe('ChatContainer Component', () => {
    let Component;

    beforeEach(() => {
        Component = 
            mount(<Provider store={store}>
                <ChatContainer />
            </Provider>);
    });

    it('should render', () => {
        expect(Component).toBeTruthy();
    });


});

这是迄今为止触发此错误的唯一文件,所以我不知道为什么.. 我不确定是否是我的业力配置中的 babel-plugin-transform-class-properties 导致了此问题:

/* eslint-disable no-var */
var path = require('path');

module.exports = function webpackConfig(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: [
            'test/components/*.spec.js',
            // 'test/test_index.js',

        ],


        preprocessors: {
            'src/js/**/*.js': ['webpack', 'sourcemap'],
            'test/**/*.spec.js': ['webpack', 'sourcemap'],
            // 'test/test_index.js': ['webpack', 'sourcemap'],
        },

        webpack: {
            devtool: 'inline-source-map',
            alias: {
                cheerio: 'cheerio/lib/cheerio',
            },
            module: {
                loaders: [{
                    test: /\.js$/,
                    loader: 'babel',
                    query: {
                        presets: ['airbnb'],
                        plugins: ["transform-class-properties"]
                    },
                    exclude: path.resolve(__dirname, 'node_modules'),
                }, {
                    test: /\.json$/,
                    loader: 'json',
                },
            ],
            },
            externals: {
                cherrio: 'window',
                'react/lib/ExecutionEnvironment': true,
                'react/lib/ReactContext': 'window',
                'react/addons': true,
            },
        },

        webpackServer: {
            noInfo: true,
        },

        plugins: [
            'karma-webpack',
            'karma-jasmine',
            'karma-sourcemap-loader',
            'karma-phantomjs-launcher',
            'karma-spec-reporter',
            'karma-chrome-launcher',
        ],

        babelPreprocessor: {
            options: {
                presets: ['airbnb'],
            },
        },

        reporters: ['spec'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['PhantomJS'],
        singleRun: false,
    });
};

这是我的 webpack 配置:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'inline-source-map',
  entry: [
    'babel-polyfill',
    'webpack-hot-middleware/client',
    './src/client/index'
  ],
  output: {
    path: path.resolve(__dirname, './static/dist'),
    filename: 'bundle.js',
    publicPath: '/dist/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development')
      }
    })
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        query: {
          plugins: [,
            [
              'react-transform', {
                transforms: [{
                  transform: ['react-transform-hmr']
                  imports: ['react'],
                  locals: ['module']
                }, {
                  transform: 'react-transform-catch-errors',
                  imports: ['react', 'redbox-react']
                }]
              }
            ]
          ]
        },
        include: [path.resolve(__dirname, 'src')]
      },
      {
        test: /\.css?$/,
        loaders: ['style', 'raw']
      }
    ]
  }
};
4

0 回答 0