简体   繁体   English

Webpack Karma使用react / addons

[英]Webpack Karma using react/addons

I have a large-ish Angular app written in Typescript generating JS files 1:1 plus external modules such as moment and React loaded off the same server. 我有一个在Typescript中编写的大型Angular应用程序,生成JS文件1:1以及外部模块,例如moment和React在同一服务器上加载。 Dependencies are handled by RequireJS. 依赖关系由RequireJS处理。

We added some basic Angular Karma tests which worked fine. 我们添加了一些基本的Angular Karma测试,它们运行良好。 This uses a duplicate RequireJS config tweaked to load the tests into Karma. 这使用重复的RequireJS配置调整将测试加载到Karma。

Now I am trying to test some React components and in the process move to Webpack. 现在我正在尝试测试一些React组件,并在此过程中转移到Webpack。 So, I have modified the Karma config to use Webpack and installed the external dependencies using npm. 所以,我修改了Karma配置以使用Webpack并使用npm安装外部依赖项。 I have spent all day trying to get this to work but I cannot find a solution which works for my setup. 我花了一整天时间试图让它工作,但我找不到适用于我的设置的解决方案。

karma.conf.js karma.conf.js

var path = require('path');

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '',


        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['jasmine', 'requirejs'],

        // list of files / patterns to load in the browser
        files: [
            'ng/*.js',
            'ng/**/*.js',
            'ng/**/tests/*.spec.js'


        ],


        // list of files to exclude
        exclude: [
                'app.js', // Old requirejs config
           ],


        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {
            '*.js': ['webpack', 'sourcemap'],
            'ng/**/*.js': ['webpack', 'sourcemap'],
            'partials/**/*.html': ['ng-html2js']
        },


   webpack: { //kind of a copy of your webpack config
      devtool: 'inline-source-map', //just do inline source maps instead of the default
      module: {
        loaders: [
          {
            test: /\.js$/,
            loader: 'babel',
            exclude: path.resolve(__dirname, 'node_modules'),
            query: {
              presets: ['airbnb']
            }
          },
          {
            test: /\.json$/,
            loader: 'json',
          },
          {
            test: /\.ts$/,
            loader: 'typescript',
          },
        ],
      },

      externals: {
        'react': true,
        'react/addons': true,
        'react/lib/ExecutionEnvironment': true,
        'react/lib/ReactContext': true
      }
    },

    webpackServer: {
      noInfo: true //please don't spam the console when running in karma!
    },


        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress'],


        // web server port
        port: 9876,


        // enable / disable colors in the output (reporters and logs)
        colors: true,


        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,


        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,


        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers: ['PhantomJS',
                   'Chrome'
                ],

        plugins: [
             'karma-webpack',
             'karma-sourcemap-loader',
             'karma-requirejs',
             'karma-ng-html2js-preprocessor',

             //'karma-firefox-launcher',
             'karma-chrome-launcher',
             'karma-phantomjs-launcher',
             'karma-jasmine'
        ],

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

        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false,

        // Concurrency level
        // how many browser should be started simultanous
        concurrency: Infinity,

    });
};

This is what I am getting: 这就是我得到的:

PhantomJS 2.1.1 (Linux 0.0.0) ERROR
  ReferenceError: Can't find variable: react
  at /vagrant/app/media/website/js/ng/chartApp.js:48060 <- webpack:/external "react/addons":1:0

How should I be setting this up? 我该如何设置呢?

This might be happening if you're using Enzyme, which uses some lazy require() calls to maintain compatibility with React 0.13 and 0.14, so Webpack isn't bundling them. 如果您使用Enzyme,可能会发生这种情况,Enzyme使用一些惰性的require()调用来保持与React 0.13和0.14的兼容性,因此Webpack不捆绑它们。

If that's the case, put this in your karma.config.js : 如果是这种情况,请将其放入您的karma.config.js

webpack: { // ...whatever else you have... externals: { 'cheerio': 'window', 'react/addons': true, 'react/lib/ExecutionEnvironment': true, 'react/lib/ReactContext': true } }

If you're not using Enzyme, this might still be a solution (at least the react/addons part). 如果你不使用酶,这可能仍然是一个解决方案(至少react/addons部分)。

See this Karma page for details. 有关详细信息,请参阅此Karma页面

Here's your first problem: 这是你的第一个问题:

"I have a large-ish Angular app written in Typescript generating JS files 1:1 plus external modules such as moment and React loaded off the same server. Dependencies are handled by RequireJS ." “我有一个在Typescript中编写的大型Angular应用程序,生成JS文件1:1 以及外部模块,例如momentReact加载到同一台服务器上。依赖关系由RequireJS处理。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM