打字稿+业力+ webpack + codecoverage无效

时间:2016-10-10 10:23:14

标签: javascript angularjs typescript webpack karma-coverage

我想按照逐步说明将angular1项目迁移到angular2。因此我在项目中添加了typescript。但现在我遇到了使用Karma,Webpack和CodeCoverage运行的问题。 单元测试都是绿色的,但代码覆盖率并未在其报告中显示“.ts”文件。

如何配置我的karma.config以使其正常工作?我已经尝试了'karma-typescript'和'karma-remap-istanbul',但我仍然坚持为所有依赖项设置正确的设置。

这是我的karma.conf.js:

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../../',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
  'node_modules/sinon/pkg/sinon-1.17.5.js',
  'node_modules/jquery/dist/jquery.min.js',
  'node_modules/jasmine-jquery/lib/jasmine-jquery.js',
  'node_modules/svg4everybody/dist/svg4everybody.min.js',
  'specs/unit/mocks/*.js',
  'specs/config/karma.import.js'
],

// list of files to exclude
exclude: [
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
  'app/**/*.ts': ['karma-typescript'],
  'specs/config/karma.import.js': ['webpack', 'sourcemap']
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['spec', 'coverage'],

coverageReporter: {
  reporters: [
    { type: 'html', subdir: '.' }
  ],
  instrumenters: {isparta: require('isparta')},
  instrumenter: {
    'app/src/**/*.js': 'isparta'
  },
  instrumenterOptions: {
    istanbul: { noCompact: true }
  },
  includeAllSources: true
},

// web server port
port: 9876,

plugins: [
  'karma-babel-preprocessor',
  'karma-chrome-launcher',
  'karma-jasmine',
  'karma-sinon',
  'karma-spec-reporter',
  'karma-coverage',
  'karma-webpack',
  'karma-sourcemap-loader',
  'karma-typescript'
],

// enable / disable colors in the output (reporters and logs)
colors: true,

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

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,

babelPreprocessor: {
  options: {
    presets: ['latest'],
    sourceMap: 'inline'
  },
  sourceFileName: function (file) {
    return file.originalPath;
  }
},

// special webpack-configuration for handling spec-files and preparing all
// modules for the code-coverage tool
webpack: {
  devtool: 'inline-source-map',
  module: {
    loaders: webpackConfig.module.loaders,
    preLoaders: [
      // {
      //   test: /\.js$/,
      //   loader: 'babel',
      //   include: /app\/src/,
      //   exclude: /node_modules|vendor/,
      //   query: {
      //     cacheDirectory: true
      //   }
      // },
      {
        test: /\.js$/,
        loader: 'istanbul-instrumenter',
        include: /app\/src/,
        exclude: /node_modules|vendor/,
        query: {
          cacheDirectory: true,
          esModules: true
        }
      }
    ]
  },
  cache: true
},

webpackMiddleware: {
  stats: {
    chunkModules: false,
    colors: true
  }
}

这是“karma.import.js”,其中所有资源都单独加载:

const testsContext = require.context('../../app/src/', true, /\.spec\.js$/); 
testsContext.keys().forEach(testsContext);

// require all `src/components/**/index.js`
const componentsContext = require.context('../../app/src/', true, /(app)\.js$/);
componentsContext.keys().forEach(componentsContext);

require('angular-mocks');

0 个答案:

没有答案