首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typescript代码在调试单元测试时启用了覆盖率

Typescript代码在调试单元测试时启用了覆盖率
EN

Stack Overflow用户
提问于 2019-10-23 16:19:20
回答 1查看 266关注 0票数 1

我有一个混合的Angular应用程序(混合在1.7.5和8.2.10之间),它使用了TypeScript和JavaScript的混合。

为了运行单元测试,我有两个独立的npm脚本,一个用于ngX测试,另一个用于ng1测试。

我的问题是,当我运行ng1测试进行调试时,typescript代码总是启用了代码覆盖率,这意味着它被缩小了,并且有额外的代码行来计算我正在命中哪些行/分支/语句/函数。这显然使调试成为一种主要的烦恼。这是我用于运行测试的npm脚本

代码语言:javascript
复制
 "test:ng1": "karma start ./src/karma.ng1.conf.js",

下面是我运行的一个配置示例,其中删除了所有覆盖范围插件,以防止覆盖范围代码应用于我的typescript:

代码语言:javascript
复制
module.exports = function(config) {

  var reporters = ['mocha', 'kjhtml'];
  // Only implement coverage if the coverage argument was set
  if(config.coverage){
    reporters.push('coverage-istanbul');
    console.log('Coverage enabled');
  }
  else{
    console.log('Coverage not enabled');
  }

  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '../',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine', 'karma-typescript'],

    preprocessors: {
      'src/app/**/*.ts': ['karma-typescript'],
      'src/test/spec/**/*.ts': ['karma-typescript'],
      // 'src/app/modules/ng1/**/*.js': ['karma-coverage-istanbul-instrumenter'],
      // 'src/app/modules/ng1/**/*.ts': ['karma-typescript', 'karma-coverage-istanbul-instrumenter'],
      '**/*.html': ['ng-html2js']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },

    // list of files / patterns to load in the browser
    files: [
      'node_modules/babel-polyfill/browser.js',
      'node_modules/jquery/dist/jquery.js',
      'node_modules/angular/angular.js',
      'node_modules/angular-mocks/angular-mocks.js',
      'node_modules/angular-animate/angular-animate.js',
      'node_modules/angular-cookies/angular-cookies.js',
      'node_modules/angular-resource/angular-resource.js',
      'node_modules/angular-sanitize/angular-sanitize.js',
      'node_modules/@uirouter/angularjs/release/angular-ui-router.js',
      'node_modules/ui-bootstrap4/dist/ui-bootstrap-tpls.js',
      'node_modules/lodash/lodash.js',
      'node_modules/angular-growl-v2/build/angular-growl.js',
      'node_modules/angular-jquery-timepicker/src/timepickerdirective.js',
      'node_modules/timepicker/jquery.timepicker.js',
      'node_modules/angularjs-scroll-glue/src/scrollglue.js',
      'node_modules/angular-translate/dist/angular-translate.js',
      'node_modules/moment/moment.js',
      'node_modules/ngstorage/ngStorage.js',
      'node_modules/jasmine-promise-matchers/dist/jasmine-promise-    matchers.js',
      'node_modules/angular-ui-grid/ui-grid.js',
      'src/app/*.ts',
      'src/app/modules/**/*.js',
      'src/app/modules/**/*.ts',
      'src/app/partials/**/*.html',
      'src/test/spec/ipsmicaTestFixtures.js',
      'src/test/spec/**/*.spec.js',
      'src/test/spec/**/*.spec.ts'
    ],

    // list of files / patterns to exclude
    exclude: [
      'src/app/modules/ngX/**/*.spec.ts'
    ],

    ngHtml2JsPreprocessor: {
      cacheIdFromPath: function(filepath) {
        return filepath.replace('src/app/partials/', '');
      },

      moduleName: 'ipsmica.templates'
    },

    // web server port defaults to 9876
    browsers: [
      'ChromeHeadless'
    ],
    browserNoActivityTimeout: 60000,

    // Code coverage report
    reporters: reporters,

    mochaReporter: {
      ignoreSkipped: true
    },

    // coverageIstanbulReporter: {
    //   reports: ['text-summary', 'html'],
    //   fixWebpackSourcePaths: true,
    //   dir: 'target/coverage/ng1',
    //   subdir: '.'
    // },

    // Which plugins to enable
    plugins: [
      'karma-chrome-launcher',
      'karma-jasmine',
      // 'karma-coverage-istanbul-reporter',
      'karma-jasmine-html-reporter',
      // 'karma-coverage-istanbul-instrumenter',
      'karma-mocha-reporter',
      'karma-ng-html2js-preprocessor',
      'karma-typescript'
    ],
    tsconfig: 'tsconfig.spec.json',
    colors: true,

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

有了这个配置,typescript代码在调试时仍然嵌入了覆盖率代码。如何禁用此功能?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-20 16:44:15

对于遇到这种情况的任何人来说,问题是karma-typescript会自动启用代码覆盖率,所以我只需要添加一个设置来禁用它并启用源代码映射

在我的报应配置中

代码语言:javascript
复制
karmaTypescriptConfig = {
  bundleOptions:{
    sourceMap: true
  },
  coverageOptions:{
    instrumentation : false
  }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58518466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档