gpt4 book ai didi

javascript - 带有业力的覆盖率报告以及 javascript 和 typescript src 文件的混合

转载 作者:搜寻专家 更新时间:2023-10-30 21:40:54 25 4
gpt4 key购买 nike

我有一个项目,我使用 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.x 和 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-loaderkarma-coverage-istanbul-reportersource-map-loader ,并且在 tsconfig.json 中,您想在 compilerOptions 中设置这些:

"inlineSourceMap": true,
"sourceMap": false,

我指出了关于 html 报告的 TODO。它确实有效,但我无法将其输出到包含 TypeScript 文件的自定义目录(子目录)。 JavaScript 只能正常工作。可能是 istanbul-reports 的特定于 Windows 的问题。如果将 html 添加到 coverageIstanbulReporter 下的 reports 数组,您应该会在项目目录中看到它,但将它放入 中可能会遇到问题REPORTS_PATH.

还值得注意的是,我很幸运地使用了 karma-remap-coverage 而不是 karma-coverage-istanbul-reporter 但前者无法正确生成cobertura 报道报道,这正是我对 Jenkins 所需要的。

关于javascript - 带有业力的覆盖率报告以及 javascript 和 typescript src 文件的混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40633826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com