覆盖报告与karma和javascript和typescript src文件的混合
Coverage reports with karma and a mix of javascript and typescript src files
我有一个项目,我使用webpack进行开发/测试,karma作为我的测试运行器。这个项目的源文件一半用js编写,一半用ts/tsx编写。测试套件完全是用js编写的。我目前使用的是karma-coverage,它显示了我所有js源文件的覆盖率报告,但它不支持typescript文件。我所有的测试都运行了,没有问题,我只是想要所有测试文件的覆盖率报告。有人能给我指个方向吗?
如果有帮助的话,这是我的karma.conf.js。
'use strict';
const webpackCfg = require('./webpack.config')('test');
module.exports = function karmaConfig(config) {
config.set({
browsers: ['Chrome'],
files: [
'test/loadtests.js'
],
port: 8080,
captureTimeout: 60000,
frameworks: [
'mocha',
'chai',
'sinon'
],
client: {
mocha: {}
},
singleRun: true,
reporters: ['mocha', 'coverage', 'junit'],
mochaReporter: {
output: 'autowatch'
},
preprocessors: {
'test/loadtests.js': ['webpack', 'sourcemap']
},
webpack: webpackCfg,
webpackServer: {
noInfo: true
},
junitReporter: {
outputDir: 'coverage',
outputFile: 'junit-result.xml',
useBrowserName: false
},
coverageReporter: {
dir: 'coverage/',
watermarks: {
statements: [70, 80],
functions: [70, 80],
branches: [70, 80],
lines: [70, 80]
},
reporters: [
{ type: 'text' },
{
type: 'html',
subdir: 'html'
},
{
type: 'cobertura',
subdir: 'cobertura'
},
{
type: 'lcovonly',
subdir: 'lcov'
}
]
}
});
};
和我的webpack测试配置的相关部分
{
devtool: 'inline-source-map',
externals: {
cheerio: 'window',
'react/lib/ExecutionEnvironment': true,
'react/addons': true,
'react/lib/ReactContext': true,
},
module: {
preLoaders: [
{
test: /'.(js|jsx)$/,
loader: 'isparta-loader',
include: [
this.srcPathAbsolute
]
}
],
loaders: [
{
test: /'.cssmodule'.css$/,
loaders: [
'style',
'css?modules&importLoaders=1&localIdentName=[name]-[local]-[hash:base64:5]'
]
},
{
test: /^.((?!cssmodule).)*'.css$/,
loader: 'null-loader'
},
{
test: /'.(sass|scss|less|styl|png|jpg|gif|mp4|ogg|svg|woff|woff2)$/,
loader: 'null-loader'
},
{
test: /'.json$/,
loader: 'json'
},
{
test: /'.ts(x?)$/,
exclude: /node_modules/,
loader: ['babel', 'ts-loader']
},
{
test: /'.(js|jsx)$/,
loader: 'babel-loader',
query: {
presets: ['airbnb']
},
include: [].concat(
this.includedPackages,
[
this.srcPathAbsolute,
this.testPathAbsolute
]
)
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"test"'
})
]
}
经过几天的灵魂和谷歌搜索数百个浏览器选项卡,我找到了一个工作的解决方案。这里使用的是TypeScript 2。和Webpack 2.x。test.js
是入口点。它也可以是test.ts
(最终会是)。在那个入口点,我加载我的*.spec.js
和*.spec.ts
文件。然后这些文件导入它们需要测试的源文件。我已经把所有的webpack配置在karma.conf.js
,所以它更容易看到:
let myArgs = require('yargs').argv;
let path = require('path');
let webpack = require('webpack');
module.exports = function(config) {
const REPORTS_PATH = myArgs.reportsPath ? myArgs.reportsPath :path.join(__dirname, 'build');
config.set({
basePath: '',
frameworks: ['jasmine', 'es6-shim'],
files: [
'./test.js'
],
exclude: [],
reporters: ['progress', 'spec', 'coverage', 'junit', 'coverage-istanbul'],
preprocessors: {
'./test.js': ['webpack', 'sourcemap']
},
webpackServer: {
noInfo: true // prevent console spamming when running in Karma!
},
webpack: {
devtool: 'inline-source-map',
resolve: {
modules: [
path.resolve('./node_modules'),
path.resolve('./')
],
extensions: ['.js', '.ts', '.css', '.scss']
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})
],
module: {
rules: [
{
enforce: 'pre',
test: /'.js$/,
use: 'source-map-loader',
exclude: [/node_modules/]
},
{
test: /'.ts$/,
use: [{
loader: 'awesome-typescript-loader',
options: {
module: 'commonjs'
},
}]
},
{
test: /'.js$/,
use: [{
loader: 'awesome-typescript-loader',
options: {
entryFileIsJs: true,
transpileOnly: true
}
}],
exclude: [/node_modules/],
},
{
enforce: 'post',
test: /'.(js|ts)$/,
use: [{
loader: 'istanbul-instrumenter-loader',
options: {
esModules: true
}
}],
exclude: [/node_modules/, /'.spec'.(js|ts)$/, /test/]
},
{ test: /'.html/, use: 'raw-loader' },
{ test: /'.(s)?css$/, use: 'null-loader' },
{ test: /'.(png|jpg|jpeg|gif|svg|pdf)$/, use: 'null-loader' },
{ test: /'.woff(2)?('?v=[0-9]'.[0-9]'.[0-9])?$/, use: 'null-loader' },
{ test: /'.(ttf|eot)('?v=[0-9]'.[0-9]'.[0-9])?$/, use: 'null-loader' },
{ test: /'.json$/, use: 'null-loader' }
]
}
},
coverageReporter: {
type: 'in-memory'
},
coverageIstanbulReporter: {
//TODO: Figure out why the 'html' reporter blows up with istanbul-reports (something with absolute path copying files)
reports: ['text-summary', 'cobertura'],
// base output directory
dir: REPORTS_PATH,
fixWebpackSourcePaths: true,
'report-config': {
cobertura: {
file: 'coverage.xml'
},
'text-summary': {
file: null
}
}
},
junitReporter: {
outputDir: `${REPORTS_PATH}/junit/`,
outputFile: 'jasmine-results.xml'
},
// Hide webpack build information from output
webpackMiddleware: {
stats: {
chunkModules: false,
colors: true
},
noInfo: 'errors-only'
},
colors: true,
logLevel: config.LOG_ERROR,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
autoWatchBatchDelay: 400
});
};
所以,这里的关键部分是awesome-typescript-loader
, karma-coverage-istanbul-reporter
, source-map-loader
,在tsconfig.json
中,你想在compilerOptions
中设置它们:
"inlineSourceMap": true,
"sourceMap": false,
我指出了html报告的TODO。它确实工作,但我无法让它输出到一个自定义目录(subdir)与TypeScript文件作为它的一部分。JavaScript只能正常工作。可能是istanbul-reports
特定于windows的问题。如果您将html
添加到coverageIstanbulReporter
下的reports
数组中,您应该在项目目录中看到它,但将其放在REPORTS_PATH
中可能会遇到问题。
同样值得注意的是,我使用karma-remap-coverage
而不是karma-coverage-istanbul-reporter
有很多运气,但前者不会正确生成覆盖的cobertura报告,这是我需要的Jenkins
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- 内容安全策略指令:;脚本src'self'blob:文件系统:chrome扩展资源:“;获取是否时
- javascript文件中带有php(wordpress)的Src文件路径
- Django,HTML,JS:<img src=“#">可以在HTML中工作,但不能在JS文件中工作
- Gulp js src :首先获取最深的文件
- Grunt 可以直接从文件对象/数组或 glob 中的“src”值通过 HTTP 加载远程文件
- 是否可以在javascript中访问
src的文件创建时间
- <img src=“;数据:..使用gzip文件
- src为null的iframe中的Javascript文件的路径
- 未加载JavaScript src文件
- 将文件编译到dest文件夹和src文件夹
- 可以't在html中打开src文件,因为路径没有'不存在
- 在我的html页面中避免使用多个src文件
- 节点服务器未收到对img元素的src文件更改请求
- 覆盖报告与karma和javascript和typescript src文件的混合
- 使用javascript更改图像src文件不会改变显示的图片
- 删除吞咽.在gulp.dest之后的SRC文件
- 如何从php中的不同src文件夹加载文件
- jquery src文件引用的带有jquery路径的C#变量不工作
- Angular gulp -如何根据每个SRC文件夹拆分dist文件夹中的js文件