【发布时间】:2017-06-22 14:14:19
【问题描述】:
我正在努力为我的 typescript/mocha/gulp 项目获得 nyc/istanbul 的适当报道。我尝试了很多方法,其中一些似乎无法使用源映射,而其他一些由于ts-node/tsc 错误而失败。我目前的设置是:
nycpackage.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.jsmocha相关部分
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
}
}
使用上述设置覆盖会为所有文件生成结果,但它们对于 TS 文件不正确,很可能是由于未使用源映射(即报告显示没有覆盖作为 cmets 的行并且数字似乎也是错误的) )。
尝试了多种变体方法但均未成功,最常见的建议之一是将"require": ["ts-node/register"] 添加到nyc 配置中,然后我收到错误抱怨,即gulpfile.js、docs/reports/coverage/lcov-report/prettify.js 和数量其他JS文件为not under 'rootDir',这是正确的,但尚不清楚为什么ts-node会尝试处理src中的所有文件,即使它们被排除在tsconfig.json中(仍然配置变得非常复杂)。
如果有任何建议可以得到正确的 TS 文件覆盖率报告,我将不胜感激。
【问题讨论】:
-
设置正确的覆盖测试有时会很麻烦。我停止在 React-Typescript 项目中使用 Mocha/Istanbul,转而使用 Jest 和 ts-jest (github.com/kulshekhar/ts-jest),它需要伊斯坦布尔以及 React 测试所需的所有好东西。这对我来说是跨不同版本的稳定堆栈。
-
感谢您的建议!虽然实际上这是我的考虑之一,但我是否正确它可能需要重写,因为
jest与mocha不同? -
摩卡测试很容易改写成笑话。删除描述部分并将 it() 更改为 test()。说法也不同。但是 before* 和 after* 部分是相同的。需要一些时间,但不长...
-
以及它如何适合(或比较与 mocha/sinon/chai)为非反应的东西编写测试?
-
就像摩卡一样简单!
标签: javascript typescript mocha.js istanbul tsconfig