gpt4 book ai didi

typescript - 使用 karma-jasmine 和 istanbul 的 Typescript 代码覆盖率

转载 作者:搜寻专家 更新时间:2023-10-30 20:31:31 24 4
gpt4 key购买 nike

我正在尝试使用 Istanbul 在 karma 框架中获取我的 typescript 代码的代码覆盖率在 karma.conf 中包含 typescript 文件,通过 karma typescript-preprocessor,我们能够对 typescript 代码进行单元测试和代码覆盖,但是代码覆盖率报告用于转码的 JavaScript 代码

如何获取 typescript 代码的覆盖率报告?

这是我的 karma.conf 文件。

module.exports = function(config) {
config.set({

// base path, that will be used to resolve files and exclude
basePath: '',


// frameworks to use
frameworks: ['jasmine'],

preprocessors: {
'src/**/*.ts': ['typescript', 'coverage'],
'test/**/*.ts': ['typescript']
},
typescriptPreprocessor: {
options: {
sourceMap: false, // (optional) Generates corresponding .map file.
target: 'ES5', // (optional) Specify ECMAScript target version: 'ES3' (default), or 'ES5'
module: 'amd', // (optional) Specify module code generation: 'commonjs' or 'amd'
noImplicitAny: true, // (optional) Warn on expressions and declarations with an implied 'any' type.
noResolve: false, // (optional) Skip resolution and preprocessing.
removeComments: true, // (optional) Do not emit comments to output.
concatenateOutput: false // (optional) Concatenate and emit output to single file. By default true if module option is omited, otherwise false.
},
// extra typing definitions to pass to the compiler (globs allowed)
// transforming the filenames
transformPath: function (path) {
return path.replace(/\.ts$/, '.js');
}

//options: {
// sourceMap: true,
//}
},

// list of files / patterns to load in the browser
files: [

'src/**/*.ts',
'test/**/*.ts'
],


// list of files to exclude
exclude: [

],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress','coverage'],


// web server port
port: 9876,


// 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, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['PhantomJS'],


// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
plugins: [
'karma-jasmine',
'karma-chrome-launcher',
'karma-phantomjs-launcher',
'karma-typescript-preprocessor',
'karma-coverage'
//require('../../../node_modules/karma-typescript-preprocessor/index.js')
]

});
};

最佳答案

安装 karma-typescript :

npm install karma-typescript --save-dev

将其放入您的 karma.conf.js 中:

frameworks: ["jasmine", "karma-typescript"],

files: [
{ pattern: "src/**/*.ts" }
],

preprocessors: {
"**/*.ts": ["karma-typescript"]
},

reporters: ["progress", "karma-typescript"],

browsers: ["Chrome"]

这将即时运行您的 Typescript 单元测试并生成如下所示的 Istanbul html 覆盖范围:

要运行上面的例子,你需要安装一些包:

npm install @types/jasmine jasmine-core karma karma-chrome-launcher karma-cli karma-jasmine karma-typescript typescript

这是单元测试 vanilla Typescript 代码的完整配置,在这种情况下不需要 tsconfig.json。对于 Angular、React 等更复杂的设置,您可以在 examples folder 中找到示例在integration tests .

关于typescript - 使用 karma-jasmine 和 istanbul 的 Typescript 代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38764929/

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