我努力为我的打字稿/ mocha/gulp项目获得适当的nyc/istanbul报道。我尝试了一些方法,其中一些似乎无法使用源地图,而其他因为ts-node
/tsc
错误而失败。我目前的设置是:获得nyc /伊斯坦布尔的覆盖面报告与打字稿一起工作
nyc
相关配置在package.json
"scripts": {
"test:coverage": "nyc npm run test:unit",
"test:unit": "gulp mocha"
}
"nyc": {
"check-coverage": true,
"all": true,
"extension": [
".js",
".jsx",
".ts",
".tsx"
],
"include": [
"src/**/!(*.test.*).[tj]s?(x)"
],
"reporter": [
"html",
"lcov",
"text",
"text-summary"
],
"report-dir": "docs/reports/coverage"
}
gulpfile.js
mocha
相关部分
const SRC_DIR = path.join(__dirname, 'src');
const SRC_FILES = path.join(SRC_DIR, '**', '*.[jt]s?(x)');
const TEST_FILES = path.join(SRC_DIR, '**', '*.test.[jt]s?(x)');
const MOCHA_CONFIG = {
src: [
TEST_FILES
],
watchSrc: [
SRC_FILES,
TEST_FILES
],
mocha: {
// compilers: [
// 'ts:ts-node/register',
// 'tsx:ts-node/register'
// ],
require: [
'./tests/setup.js',
'ignore-styles',
'source-map-support/register'
]
}
};
gulp.task('mocha', mocha(MOCHA_CONFIG));
tsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"rootDir": "./src",
"outDir": "./build",
"allowJs": true,
"module": "commonjs",
"target": "es5",
"lib": ["es5", "es6", "dom"],
"sourceMap": true,
"inlineSourceMap": false,
"inlineSources": false,
"experimentalDecorators": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"jsx": "react",
"moduleResolution": "node"
},
"exclude": [
"docs",
"tests",
"**/*.test.js",
"**/*.test.jsx",
"**/*.test.ts",
"**/*.test.tsx",
"tools",
"gulpfile.js",
"node_modules",
"build",
"typings/main",
"typings/main.d.ts"
],
"awesomeTypescriptLoaderOptions": {
"useCache": true,
"useBabel": true
}
}
通过以上的设置覆盖produc es结果的所有文件,但它们对TS文件不正确很可能是由于未使用源地图(即,报告显示没有覆盖的行是评论和数字似乎也是错误的)。
拥有一批变种的办法尝试没有成功的最常用的建议之一是增加"require": ["ts-node/register"]
到nyc
配置但随后遇到错误抱怨即gulpfile.js
,docs/reports/coverage/lcov-report/prettify.js
和一些其他的JS文件是not under 'rootDir'
哪些是正确的,但它不清楚为什么ts-node
试图处理所有文件中的src
,即使它们被排除在tsconfig.json
(仍然配置变得非常复杂)。
我会很感激任何建议,哪些方式去获得适当的覆盖率报告TS文件。
使用覆盖设置正确的测试有时会很麻烦。我停止使用Mocha/Istanbul作为React-Typescript项目,并切换到Jest和ts-jest(https://github.com/kulshekhar/ts-jest),它随伊斯坦布尔要求提供,以及React测试所需的所有好东西。这对我来说是一个跨越不同版本的稳定堆栈。 –
感谢您的建议!虽然实际上这是我的考虑之一,我是否对它可能需要重写,因为'jest'不同于'mocha'? – ciekawy
摩卡测试很容易被改写成笑话。删除描述部分并将其更改()为test()。这些断言也是不同的。但之前*和之后*部分是相同的。需要一段时间,但是长时间... –