我想按照分步说明将 angular1 项目迁移到 angular2。因此,我在项目中添加了打字稿。但现在我在使用 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');