简体   繁体   English

Karma Coverage和Babel + Browserify预处理

[英]Karma Coverage and Babel+Browserify Preprocessing

I'm using Karma to test my ES6 code. 我正在使用Karma来测试我的ES6代码。 When I add karma-coverage to the mix, I need to add all the source files for the coverage tool to make a useful report, but when I do that, Karma gives me this error in all browsers: 当我将karma-coverage添加到混合中时,我需要为coverage工具添加所有源文件以生成有用的报告,但是当我这样做时,Karma在所有浏览器中都给出了这个错误:

PhantomJS 1.9.8 (Mac OS X 0.0.0) ERROR PhantomJS 1.9.8(Mac OS X 0.0.0)错误

Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element. 错误:不变违规:_registerComponent(...):目标容器不是DOM元素。

at /var/folders/55/9_128mq95kz1q_2_vwy7qllw0000gn/T/41cf272955d73fbba8ad1df941172139.browserify:46444:0 <- ../../node_modules/react/lib/invariant.js:49:0 at /var/folders/55/9_128mq95kz1q_2_vwy7qllw0000gn/T/41cf272955d73fbba8ad1df941172139.browserify:46444:0 < - ../../node_modules/react/lib/invariant.js:49:0

My Karma config file is: 我的Karma配置文件是:

basePath: '',
browserNoActivityTimeout: 100000,
frameworks: ['phantomjs-shim', 'mocha', 'chai', 'browserify'],
files: [
  './client/**/*.js',
  './client/**/*.spec.js'
],
exclude: [
  './client/dist/*.js',
],
preprocessors: {
  './client/**/*.js': ['browserify', 'sourcemap', 'coverage']
},
browserify: {
  debug: true,
  transform: [
    ['babelify', {
        optional: ["runtime"],
        plugins: ["rewire"]
    }],
  ]
},
coverageReporter: {
  instrumenters: { isparta : require('isparta') },
  instrumenter: {
    '**/*.js': 'isparta'
  },
  type : 'html',
  dir : './coverage/'
},
reporters: ['mocha', 'coverage'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome', 'Firefox', 'Safari', 'PhantomJS'],
singleRun: true

If I remove './client/**/*.js', from the files array, the tests work, but then the coverage only show me the tests code. 如果我从files数组中删除'./client/**/*.js',测试工作正常,但覆盖范围只显示测试代码。 I use Karma from gulp with gulp-karma , but I suppose that this doesn't have anything to do with the problem. 我从gulp gulp-karma laarma中gulp-karma ,但我认为这与问题没有任何关系。

I had that same problem, which in my case occurred because React couldn't find the element in which it needed to render the html. 我有同样的问题,在我的情况下发生,因为React找不到它需要渲染html的元素。

I found a quick fix by adding the following if statement into my main js file: 我通过在主js文件中添加以下if语句找到了一个快速修复:

if ($('#container').length <= 0) {
  $('body').prepend('<div id="container"></div>');
}

ReactDom.render(
  <App />,
  document.getElementById('container')
);

I'm aware this must not be the best way of fixing it, but at least it works for now. 我知道这不一定是修复它的最好方法,但至少它现在有效。 If anyone knows of a better way, please let us know! 如果有人知道更好的方法,请告诉我们!

Code you are covering is trying to render component into DOM node. 您要覆盖的代码是尝试将组件呈现到DOM节点中。 Your code relys that it already exists (somewhere in index.html or whatever). 您的代码依赖于它已经存在(在index.html或其他任何地方)。 But PhantomJS cannot find that DOM node. 但是PhantomJS无法找到那个DOM节点。 You should create it before calling ReactDOM.render or search how to change template of html page used by phantom to run tests (there are plugins doung this). 您应该在调用ReactDOM.render之前创建它,或者搜索如何更改幻像使用的html页面的模板来运行测试(这里有插件)。

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

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