9

我一直试图运行几天的业力覆盖现在只能找到一个空的空白页面如下。 Karma coverage empty report噶覆盖总是空

这里是我的配置:

var path = require('path'); 

var webpackConfig = require('./webpack.common'); 

module.exports = function (config) { 
    var _config = { 
    basePath: '', 
    frameworks: ['jasmine'], 
    files: [ 
     { pattern: './karma-shim.js', watched: false } 
    ], 
    exclude: [], 
    preprocessors: { 
     './karma-shim.js': ['webpack', 'sourcemap', 'coverage'] 
    }, 
    client: { 
     captureConsole: false 
    }, 
    webpack: webpackConfig, 

    webpackMiddleware: { 
     stats: 'errors-only' 
    }, 

    coverageReporter: { 
     dir: 'coverage/', 
     reporters: [{ 
     type: 'json', 
     dir: 'coverage', 
     subdir: 'json', 
     file: 'coverage-final.json' 
     }] 
    }, 

    remapIstanbulReporter: { 
     src: 'coverage/json/coverage-final.json', 
     reports: { 
     lcovonly: 'coverage/json/lcov.info', 
     html: 'coverage/html', 
     'text': null 
     }, 
     timeoutNotCreated: 1000, // default value 
     timeoutNoMoreFiles: 1000 // default value 
    }, 

    webpackServer: { 
     noInfo: true // please don't spam the console when running in karma! 
    }, 
    reporters: ["mocha", "coverage", "karma-remap-istanbul"], 
    port: 9876, 
    colors: true, 
    logLevel: config.LOG_ERROR, 
    autoWatch: false, 
    browsers: ['PhantomJS'], // you can also use Chrome 

    singleRun: true 
    }; 

    config.set(_config); 

}; 

这是我的人缘,shim.js文件

Error.stackTraceLimit = Infinity; 
require('es6-shim'); 
require('reflect-metadata'); 
require('ts-helpers'); 
require('zone.js/dist/zone'); 
require('zone.js/dist/long-stack-trace-zone'); 
require('zone.js/dist/jasmine-patch'); 
require('zone.js/dist/async-test'); 
require('zone.js/dist/fake-async-test'); 

var appContext = require.context('./app', true, /\.spec\.ts/); 
appContext.keys().forEach(appContext); 

var testing = require('@angular/core/testing'); 
var browser = require('@angular/platform-browser-dynamic/testing'); 

testing.setBaseTestProviders(browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); 

的文件夹结构如下: folder structure

任何想法,我是什么?在这里失踪?非常感谢。

感谢

+0

你尝试提供信号源(不需要测试文件)文件纳“预处理器”?我想你错过了。 – TypeScripter

+0

哪一个?你能否详细说明一下? –

+0

在你的karma.conf.js中你有一个“预处理器”标签..我只在那里看到垫片文件。您还需要为源代码提供路径。覆盖始终是为源代码生成的。所以它应该有'app/**/*。js'之类的东西...我不认为你需要shim文件..所以删除它并尝试只用源文件。 – TypeScripter

回答

1

你的业障的配置显然是缺少源的参考。

请修改配置如下:

module.exports = function (config) { 
    var _config = { 
     [...] 
     preprocessors: { 
      // Remove coverage as preprocessor for your tests - 
      // Istambul (runs coverage behind the scene for karma) will 
      // instrumentate this 
      './karma-shim.js': ['webpack', 'sourcemap'], 
      // Add path to your source (.js or .ts - depands on your project) 
      './index.ts': [ 'coverage' ] 
     }, 
     [...] 
    }, 
    [...] 
} 

说明:覆盖测试你的代码对你的单元测试 - 你需要为你的代码来获得噶分析覆盖提供切入点。

额外 - 增加人缘覆盖插件:

module.exports = function (config) { 
    var _config = { 
     [...] 
     plugins: [ 
      require('karma-coverage') 
     ], 
     [...] 
    }, 
    [...] 
} 

附加功能 - 卡玛&打字稿文件:

module.exports = function (config) { 
    var _config = { 
     [...] 
     plugins: [ 
      require('@angular/cli/plugins/karma') 
     ], 
     [...] 
    }, 
    [...] 
    preprocessors: { 
     './src/test.ts': [ '@angular/cli' ], 
     './index.ts': [ 'coverage' ] 
    }, 
    [...] 
    mime: { 
     'text/x-typescript': [ 'ts', 'tsx' ] 
    }, 
    [...] 
} 
+0

'coverage'预处理器是否引用了“karma-coverage”插件? 如果是这样,你如何使业力覆盖处理TypeScript文件?它似乎只处理JavaScript文件。 –

+0

@SaadBenbouzid你需要导入karma-coverage插件 - 我已经更新了上面的答案。卡玛将从那里拿走它。当谈到打字稿时,你显然需要使用一些额外的预处理器,例如。 karma-tsc-preprocessor(我没有测试过这个!)或者我在日常角度项目中使用angular-cli。还添加到上面的答案。希望这会有所帮助;-) – Fill