Karma覆盖率总是显示100%

5

我的 karma.conf.js 文件看起来像这样:

// Karma configuration
// Generated on Tue Jun 11 2013 14:14:12 GMT+0100 (GMT Daylight Time)


// base path, that will be used to resolve files and exclude
basePath = '';


// list of files / patterns to load in the browser
files = [
    JASMINE,
    JASMINE_ADAPTER,
    '../Scripts/angular/angular.js',
    '../Scripts/angular/restangular/underscore-min.js',
    '../Scripts/angular/restangular/restangular-min.js',
    '../Scripts/angular/angular-*.js',
    '../Scripts/angular/angular-test/angular-*.js',
    '../Scripts/angular/angular-ui/*.js',
    '../Scripts/angular/angular-strap/*.js',
    '../Scripts/angular/angular-http-auth/*.js',
    '../uifw/scripts/ui-framework-angular.js',
    '../app/app.js',
    '../app/**/*.js',
    'unit/**/*.js'
];


// list of files to exclude
exclude = [
  '../Scripts/angular/angular-test/angular-scenario.js'
];

preprocessors = {
  '**/../app/**/*.js': 'coverage'
};



coverageReporter = {
    type: 'html',
    dir: 'coverage/'
};


// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters = ['progress', 'coverage'];


// web server port
port = 9876;


// cli runner port
runnerPort = 9100;


// enable / disable colors in the output (reporters and logs)
colors = true;


// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_DEBUG;


// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['Chrome'];


// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;

我的文件夹结构如下:

Root
 |__ App
 |__ Scripts
 |__ Tests
 |__ .... other folders

Karma.conf.js位于tests文件夹内。从tests文件夹内运行Karma start karma.conf.j

我的测试运行了,创建了一个coverage文件夹,但覆盖率总是显示为100%。

Coverage

我做错了什么?

编辑:

事实上,答案很简单。 preprocessors = { '**/../app/**/*.js': 'coverage' }; 不再需要前缀**

更多详情请参见此处

3个回答

2

使用istanbul-instrumenter-loader设置webpack可以帮助您走上正确的轨道。

{ test: /\.ts/, include: helpers.root('src', 'app'), loader: 'istanbul-instrumenter-loader', enforce: 'post' }

0

您需要设置ANGULAR_SCENARIO_ADAPTERKarma不使用JASMINE_ADAPTER


1
我认为你把单元测试和端到端测试搞混了。我的问题是关于单元测试的。请看我的编辑,里面有对我有效的答案。 - Simon Lomax
我有同样的问题,我认为这不是适配器的问题。Jasmine测试实际上正在执行,并且Karma确实使用了jasmine适配器。问题在于覆盖率报告始终为100%。 - joragupra

0
在我看来,你的问题似乎是找不到任何你试图生成覆盖率的文件。你的报告显示0/0行被运行,0/0语句等等。
我曾经遇到过类似的问题,那是因为我使用小写字母而不是大写字母,而且看起来你可能也有同样的问题,因为你有一个名为“App”的文件夹,但在配置中引用它时使用了“app”。尝试通过编写** / .. / App / ** / * .js':'coverage'来使其正常工作。
此外,我不确定,但我认为你应该写../App/**/*.js': 'coverage'而不是** / .. / App / ** / * .js':'coverage'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接