简体   繁体   English

如何使用isparta从karma中的代码覆盖中排除第三方导入(如jquery)?

[英]How do I exclude third party imports like jquery from code coverage in karma using isparta?

I'm using karma, webpack, and jasmine to test my ES6 code, with istanbul and isparta for code coverage. 我正在使用karma,webpack和jasmine来测试我的ES6代码,使用istanbul和isparta进行代码覆盖。 My tests all seem to pass, but the coverage is really low because I had to import jquery and jquery-resizable-dom, which gets included in the coverage as untested code. 我的测试似乎都通过了,但覆盖率非常低,因为我必须导入jquery和jquery-resizable-dom,它作为未经测试的代码包含在coverage中。

I have a lot of vanilla js code (~200 lines) but then added something like this: 我有很多香草js代码(约200行),但后来添加了这样的东西:

import $ from 'jquery';
require('imports?jQuery=jquery!jquery-resizable-dom');

const makeImagesResizable = () => {
  $('.img').resizable({
    handleSelector: '> .glyphicon-resize-full'
  });
};

export { makeImagesResizable };

And in my jasmine test, I imported makeImagesResizable to test it. 在我的茉莉花测试中,我导入了makeImagesResizable来测试它。 When I run the test, I get something like 27% code coverage because it includes jquery code in the coverage. 当我运行测试时,我获得了27%的代码覆盖率,因为它在coverage中包含了jquery代码。 Removing the whole code block above (the jquery import and all the code that uses jquery) bumps the coverage to 94% which is closer to the actual coverage (the jquery code is pretty short). 删除上面的整个代码块(jquery导入和使用jquery的所有代码)将覆盖率提高到94%,这更接近实际覆盖率(jquery代码非常短)。

Is there a way to exclude third party imports from the code coverage to reflect the numbers we actually get from the code? 有没有办法从代码覆盖中排除第三方导入以反映我们实际从代码中获得的数字?

Here is my karma config also: 这是我的业力配置:

const webpack = require('webpack');
const isparta = require('isparta');

module.exports = (config) => {
  config.set({
    frameworks: ['jasmine'],
    files: [
      './spec/*.spec.js',
    ],
    webpack: {
      module: {
        loaders: [
          { test: /\.js$/, loader: 'imports?define=>false!babel' },
        ],
      },
    },
    webpackMiddleware: {
      noInfo: true,
    },
    preprocessors: {
      './spec/*.spec.js': ['webpack', 'coverage'],
    },
    reporters: ['progress', 'coverage'],
    coverageReporter: {
      instrumenters: { isparta },
      reporters: [
        {
          type: 'html',
          dir: 'coverage',
        },
      ],
    },
    browsers: ['Chrome'],
    plugins: [
      'karma-webpack',
      'karma-jasmine',
      'karma-coverage',
      'karma-chrome-launcher',
    ],
  });
};

I only included test files since including the source files doesn't return any coverage at all for some reason. 我只包括测试文件,因为包含源文件由于某种原因根本不返回任何覆盖。

http://karma-runner.github.io/1.0/config/configuration-file.html引用他们的指南,您可以使用exclude选项来防止它们影响您的结果。

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

相关问题 Sourcemap + istanbul / isparta代码覆盖率为webpack + babel(对于es6)+ mocha(+ karma) - Sourcemap + istanbul/isparta code coverage for a webpack + babel (for es6) + mocha (+karma) 如何从我的代码覆盖范围中排除“node_modules”? - How do I exclude "node_modules" from my code coverage? Javascript-如何检查电影是否完成? 同时使用jQuery和其他第三方库? - Javascript - how do i check if the movie is finished? while using jQuery and other third party libraries? 如何使用从第三方脚本导入的正确调用 js 模块 - How to correctly call js modules with imports from third party scripts 如何通过Firebase调用第三方API? - How do I call third party APIs from with firebase? 使用RequireJS时如何从CDN加载第三方JavaScript? - How do I load third party JavaScript from a CDN when using RequireJS? 如何使用业力在我的Aurelia应用程序中获得代码覆盖率结果? - How can I get code coverage results in my Aurelia app using karma? 业力和CoffeScript和代码覆盖率 - Karma and CoffeScript and Code Coverage 如何从Code Coverage Istanbul Reporter中排除模拟文件 - How to exclude mock files from Code Coverage Istanbul Reporter 如何使用Require JS配置带有第三方js依赖项的日期选择器? - How do I configure a date picker with third party js dependencies using Require JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM