简体   繁体   English

使用ES6 / Babel进行Karma测试React

[英]Karma Testing React with ES6/Babel

I am trying to test my project using karma as my test runner, using the webpack babel-loader. 我正在尝试使用webpack babel-loader使用业力作为测试运行者来测试我的项目。 The code all builds successfully (i think? or the CLI says it does, but when i open the browser all files fail immedialey on the es6 imports. Any idea what i'm doing wrong? 所有代码都可以成功构建(我认为?或CLI表示可以),但是当我打开浏览器时,es6导入中的所有文件都立即失败。我知道我在做什么错吗?

var path = require('path');

module.exports = function(config) {
    config.set({
    basePath: '',
    // frameworks: ['jasmine', 'karma-webpack'],

    files: [
        'lib/bundle.js'
    ],

    exclude: ['*.styl'],

    preprocessors: {
        'test/**/*.js': ['webpack'],
        'test/**/*.jsx': ['webpack'],
        'src/**/*.jsx': ['webpack'],
        'src/**/*.js': ['webpack']
    },

    plugins: [
        require("karma-webpack")
    ],

    webpack: {
        entry: './src/entry.js',
         module: {
             loaders: [
                {
                    test: path.join(__dirname, 'src'),  
                    loader: 'babel-loader?stage=0&optional=runtime',
                    excludes: /node_modules/,
                    options: {
                        optional: ['runtime']
                    }
                }, 
                {
                    test: /\.styl$/,
                    loaders: ['style-loader', 'css-loader', 'stylus-loader']
                }
            ]
        },
        webpackMiddleware: {
          noInfo: true
        }
    }
});
};

10 11 2015 13:16:47.156:INFO [karma]: Delaying execution, these browsers are not ready: Chrome 46.0.2490 (Windows 8.1 0.0.0) Chrome 46.0.2490 (Windows 8.1 0.0.0) ERROR You need to include some adapter that implements karma .start method! 10 11 2015 13:16:47.156:INFO [karma]:延迟执行,这些浏览器尚未就绪:Chrome 46.0.2490(Windows 8.1 0.0.0)Chrome 46.0.2490(Windows 8.1 0.0.0)错误您需要包括一些实现karma .start方法的适配器!

Chrome 46.0.2490 (Windows 8.1 0.0.0) ERROR You need to include some adapter that implements karma .start method! Chrome 46.0.2490(Windows 8.1 0.0.0)错误您需要包括一些实现karma .start方法的适配器!

浏览器错误在这里

You're including the transpiled files in your test. 您将在测试中包含转译的文件。 See for example this line in your error: 例如,在您的错误中看到以下行:

WARNING in ./~/routes/dist/routes.js
Critical dependencies:
1:406-413 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
@ ./~/routes/dist/routes.js 1:406-413

Why is a dist file included? 为什么包含dist文件? You should only be including src files and dependencies. 您应该只包括src文件和依赖项。

files: [
    '../node_modules/react/react.js',
    '../node_modules/babel-polyfill/dist/polyfill.js',
    '**/*.js',
    '**/*.jsx'
],

The last two don't look right. 最后两个看起来不正确。 You're telling it to load all js/jsx files.. including the dist ones. 您要告诉它加载所有js/jsx文件..包括dist文件。

Here is the final file that works. 这是有效的最终文件。

Two things I was doing wrong (1 as noted in the comments below): 我做错了两件事(下面的评论中提到了1):

  1. I was adding untranspiled code to the files array, which of course breaks in the browser. 我向文件数组中添加了未编译的代码,这当然在浏览器中会中断。 From Karma's documents, "Files is a list of files/patterns to load in the browser,' so of course it broke on jsx on es6 tokens. 从Karma的文档中,“文件是要在浏览器中加载的文件/模式的列表,”因此,它当然在esx令牌上的jsx上中断了。

  2. I wasn't selecting jasmine as my framework so the Karma runner had no idea where to start which is why I was getting the start method error. 我没有选择茉莉花作为我的框架,所以业力赛跑者不知道从哪里开始,这就是为什么我遇到启动方法错误的原因。

  3. Lastly, I needed to include my tests in the files array, otherwise Karma will not load my tests and run them. 最后,我需要将测试包含在文件数组中,否则Karma不会加载并运行测试。 ( Note: the tests must be included in the preproccssor array as well ). 注意:测试也必须包含在preproccssor数组中 )。

     var path = require('path'); module.exports = function(config) { config.set({ basePath: '', frameworks: ['jasmine'], files: [ 'lib/bundle.js', 'test/**/*.jsx', 'test/**/*.js' ], exclude: ['*.styl'], preprocessors: { 'test/**/*.js': ['webpack'], 'test/**/*.jsx': ['webpack'], 'src/**/*.jsx': ['webpack'], 'src/**/*.js': ['webpack'] }, webpack: { entry: './src/entry.js', module: { loaders: [ { test: /\\.js$|.jsx$/, loader: 'babel-loader?stage=0&optional=runtime', excludes: /node_modules/ }, { test: /\\.styl$/, loaders: ['style-loader', 'css-loader', 'stylus-loader'] } ] }, webpackMiddleware: { noInfo: true } } }); }; 

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

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