运行业力/酶测试时,只允许安装一次babel polyfill

时间:2016-09-15 01:26:23

标签: reactjs karma-runner babel enzyme babel-polyfill

当我用酶进行业力测试时,我得到一个错误:

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();
    });


});

这是迄今为止唯一触发此错误的文件,所以我不知道为什么.. 我不确定它是否是我的karma配置中的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']
      }
    ]
  }
};

0 个答案:

没有答案