单元测试 – 使用角度2配置karma webpack的覆盖范围

前端之家收集整理的这篇文章主要介绍了单元测试 – 使用角度2配置karma webpack的覆盖范围前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用angular2 webpack设置karma-coverage?

我从棱角分明的快速启动webpack指南.但覆盖工具是空白的,不显示我的测试.谢谢!

我的文件夹结构是

  1. project
  2. |--src (project files)
  3. |--tests (all my testfiles)

我的webpack.test.js看起来像这样

  1. var helpers = require('./helpers');
  2.  
  3. module.exports = {
  4. devtool: 'inline-source-map',resolve: {
  5. extensions: ['','.ts','.js']
  6. },module: {
  7. loaders: [
  8. {
  9. test: /\.ts$/,loaders: ['ts']
  10. },{
  11. test: /\.html$/,loader: 'null'
  12.  
  13. },{
  14. test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,loader: 'null'
  15. },{
  16. test: /\.css$/,exclude: helpers.root('src','app'),include: helpers.root('src',loader: 'null'
  17. }
  18. ]
  19. }
  20. }

我的Karma.conf.js

  1. var webpackConfig = require('./webpack.test');
  2.  
  3. module.exports = function (config) {
  4. var _config = {
  5. basePath: '',frameworks: ['jasmine','sinon'],files: [
  6. {pattern: './config/karma-test-shim.js',watched: false}
  7. ],preprocessors: {
  8. './config/karma-test-shim.js': ['webpack','sourcemap']
  9. },plugins:[
  10. require('karma-jasmine'),require('karma-coverage'),require('karma-webpack'),require('karma-phantomjs-launcher'),require('karma-sourcemap-loader'),require('karma-mocha-reporter'),require('karma-sinon')
  11. ],coverageReporter: {
  12. type : 'html',dir : 'coverage/'
  13. },webpack: webpackConfig,webpackMiddleware: {
  14. stats: 'errors-only'
  15. },webpackServer: {
  16. noInfo: true
  17. },reporters: ['mocha','coverage'],port: 9876,colors: true,logLevel: config.LOG_INFO,autoWatch: true,browsers: ['PhantomJS'],singleRun: false
  18. };
  19.  
  20. config.set(_config);
  21. };

和karma-test-shim.js

  1. Error.stackTraceLimit = Infinity;
  2.  
  3. require('core-js/es6');
  4. require('reflect-Metadata');
  5.  
  6. require('zone.js/dist/zone');
  7. require('zone.js/dist/long-stack-trace-zone');
  8. require('zone.js/dist/proxy');
  9. require('zone.js/dist/sync-test');
  10. require('zone.js/dist/jasmine-patch');
  11. require('zone.js/dist/async-test');
  12. require('zone.js/dist/fake-async-test');
  13.  
  14. var testContext = require.context('../tests',true,/\.spec\.ts/);
  15.  
  16. testContext.keys().forEach(testContext);
  17.  
  18. var testing = require('@angular/core/testing');
  19. var browser = require('@angular/platform-browser-dynamic/testing');
  20.  
  21. testing.TestBed.initTestEnvironment(browser.BrowserDynamicTestingModule,browser.platformBrowserDynamicTesting());
Yuu需要将下面的代码覆盖插件导入到插件
  1. require('karma-coverage-istanbul-reporter'),

另外,使用下面的代码添加istanbul报告属性如下

  1. coverageIstanbulReporter: {
  2. reports: ['html','lcovonly'],fixWebpackSourcePaths: true,// enforce percentage thresholds
  3. // anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode
  4. thresholds: {
  5. emitWarning: false,// set to `true` to not fail the test command when thresholds are not met
  6. global: { // thresholds for all files
  7. statements: 80,lines: 80,branches: 80,functions: 80
  8. },each: { // thresholds per file
  9. statements: 80,functions: 80,overrides: {}
  10. }
  11. }
  12. },

karma.config.js的完整配置应该如下:

  1. // Karma configuration file,see link for more information
  2. // https://karma-runner.github.io/0.13/config/configuration-file.html
  3.  
  4. module.exports = function(config) {
  5. config.set({
  6. basePath: '','@angular/cli'],plugins: [
  7. require('karma-jasmine'),require('karma-chrome-launcher'),require('karma-jasmine-html-reporter'),require('karma-coverage-istanbul-reporter'),require('@angular/cli/plugins/karma'),require('karma-htmlfile-reporter'),],client: {
  8. clearContext: false // leave Jasmine Spec Runner output visible in browser
  9. },coverageIstanbulReporter: {
  10. reports: ['html',// enforce percentage thresholds
  11. // anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode
  12. thresholds: {
  13. emitWarning: false,// set to `true` to not fail the test command when thresholds are not met
  14. global: { // thresholds for all files
  15. statements: 80,functions: 80
  16. },each: { // thresholds per file
  17. statements: 80,overrides: {}
  18. }
  19. }
  20. },angularCli: {
  21. environment: 'dev'
  22. },// reporters: config.angularCli && config.angularCli.codeCoverage ? ['spec','karma-remap-istanbul'] : ['spec'],// reporters: ['mocha'],reporters: ['progress','html','kjhtml'],htmlReporter: {
  23. outputFile: 'testreports/report.html',// Optional
  24. pageTitle: 'Rest Reports',subPageTitle: 'Suite-wise report ',groupSuites: true,useCompactStyle: true,useLegacyStyle: false
  25. },browsers: ['Chrome'],singleRun: false
  26. });
  27. };

猜你在找的Angularjs相关文章