2017-06-22 80 views
2

我努力为我的打字稿/ 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.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 
    } 
} 

通过以上的设置覆盖produc es结果的所有文件,但它们对TS文件不正确很可能是由于未使用源地图(即,报告显示没有覆盖的行是评论和数字似乎也是错误的)。

拥有一批变种的办法尝试没有成功的最常用的建议之一是增加"require": ["ts-node/register"]nyc配置但随后遇到错误抱怨即gulpfile.jsdocs/reports/coverage/lcov-report/prettify.js和一些其他的JS文件是not under 'rootDir'哪些是正确的,但它不清楚为什么ts-node试图处理所有文件中的src,即使它们被排除在tsconfig.json(仍然配置变得非常复杂)。

我会很感激任何建议,哪些方式去获得适当的覆盖率报告TS文件。

+0

使用覆盖设置正确的测试有时会很麻烦。我停止使用Mocha/Istanbul作为React-Typescript项目,并切换到Jest和ts-jest(https://github.com/kulshekhar/ts-jest),它随伊斯坦布尔要求提供,以及React测试所需的所有好东西。这对我来说是一个跨越不同版本的稳定堆栈。 –

+0

感谢您的建议!虽然实际上这是我的考虑之一,我是否对它可能需要重写,因为'jest'不同于'mocha'? – ciekawy

+0

摩卡测试很容易被改写成笑话。删除描述部分并将其更改()为test()。这些断言也是不同的。但之前*和之后*部分是相同的。需要一段时间,但是长时间... –

回答

3

最近我在tsconfig.jsoncompilerOptions使用"target": "es6",而不是es5找到令人满意的解决方案。而直接在tsconfig.json改变target因为它影响构建可能不是一种选择,其他的提示是使用TS_NODE_COMPILER_OPTIONS='{"target":"es6"}它可以直接在package.jsonscripts被添加作为即:

"test:coverage": "TS_NODE_COMPILER_OPTIONS='{\"target\":\"es6\"}' nyc npm run test:unit", 

其中test:unit是任何方式用于运行。实际测试(在我的情况只是gulp mocha

注:我也更新nyc到最新的11.1.0和ts-node3.3.0https://github.com/istanbuljs/nyc/issues/618线程的建议

相关问题