我正在努力为我的打字稿/ mocha / gulp项目获得适当的nyc / istanbul报道.我尝试了很多方法,其中一些似乎无法使用源映射,而其他方法由于ts-node / tsc错误而失败.我目前的设置是:
在package.json中的nyc相关配置
"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 } }
使用上述设置覆盖产生所有文件的结果,但它们对于TS文件是不正确的,这很可能是由于未使用源映射(即报告显示没有覆盖作为注释的行并且数字似乎也是错误的).
尝试了许多变种方法没有成功,最常见的建议之一是将“require”:[“ts-node / register”]添加到nyc配置然后我收到错误抱怨ie gulpfile.js,docs /reports/coverage/lcov-report/prettify.js和不在’rootDir’下的其他JS文件的数量是正确的但是不清楚为什么ts-node尝试处理src之外的所有文件,即使它们是在tsconfig.json中排除(仍然配置变得非常复杂).
我将不胜感激任何建议,以获得适当的TS文件覆盖率报告.
解决方法@H_404_22@
最近我在tsconfig.json的compilerOptions中使用“target”:“es6”而不是es5找到了一个令人满意的解决方案.虽然直接在tsconfig.json中更改目标可能不是一个选项,因为它会影响构建,但另一个提示是使用TS_NODE_COMPILER_OPTIONS ='{“target”:“es6”},它可以直接添加到package.json脚本中,如:
"test:coverage": "TS_NODE_COMPILER_OPTIONS='{\"target\":\"es6\"}' nyc npm run test:unit",
测试:单位是用于运行实际测试的任何方式(在我的情况下只是gulp mocha.
注意:我已经按照https://github.com/istanbuljs/nyc/issues/618线程的建议将nyc更新到最新的11.1.0和ts节点更新到3.3.0
"test:coverage": "TS_NODE_COMPILER_OPTIONS='{\"target\":\"es6\"}' nyc npm run test:unit",
测试:单位是用于运行实际测试的任何方式(在我的情况下只是gulp mocha.
注意:我已经按照https://github.com/istanbuljs/nyc/issues/618线程的建议将nyc更新到最新的11.1.0和ts节点更新到3.3.0