问题
How do you setup karma test runner to generate code coverage reports of a typescript project?
Given the following folder structure and karma.conf.js file I'm already using karma to run my tests written in TypeScript.
I already fiddled around with karma-coverage
and remap-istanbul
but without any luck yet. If possible I'd like to do it without any additional npm scripts
.
.
├── karma.conf.js
├── package.json
├── src
│ └── add.ts
├── test
│ └── addSpec.ts
├── tsconfig.json
├── typings
│ ├── globals
│ └── index.d.ts
└── typings.json
karma.conf.js
var istanbul = require('browserify-istanbul');
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha', 'chai', 'sinon', 'browserify'],
files: [
'test/**/*Spec.ts'
],
exclude: [
],
preprocessors: {
'test/**/*Spec.ts': ['browserify']
},
browserify: {
debug: true,
plugin: ['tsify'],
transform: [
istanbul({irgnore: ['**/node_modules/**']})
]
},
reporters: ['progress', 'coverage']
})
}
Update 1:
I made some progress by adding browserify-istanbul
to the setup. I think the overall metrics are fine but the source file view is a bit odd.
addSpec.ts
import { add } from '../src/add'
const expect = chai.expect
describe('test add module', () => {
it('should add 2 numbers', () => {
expect(add(2, 2)).to.be.equal(4)
})
})
Update 2:
Until today I could not figure out a way to create an "integrated" karma setup with browserify and typescript. Nevertheless I have a different solution that is working for me.
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['source-map-support', 'mocha'],
files: [
'test/**/*Spec.ts'
],
exclude: [],
preprocessors: {
'test/**/*Spec.ts': ['webpack']
},
webpack: {
devtool: 'inline-source-map',
resolve: {
extensions: ['', '.ts', '.js']
},
module: {
loaders: [
{ test: /\.ts$/, exclude: /node_modules/, loader: 'ts-loader', query: { compilerOptions: { inlineSourceMap: true }} }
],
postLoaders: [
{ test: /\.ts$/, include: /src/, loader: 'istanbul-instrumenter' }
]
}
},
webpackMiddleware: {
noInfo: true
},
reporters: ['progress', 'coverage'],
coverageReporter: {
dir: 'coverage',
reporters: [
{ type: 'json', subdir: '.', file: 'coverage.json' }
]
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Electron'],
singleRun: false,
concurrency: Infinity
})
}
package.json
{
...
"scripts": {
"test": "rimraf coverage && karma start --single-run && npm run coverage",
"coverage": "npm run coverage:remap && npm run coverage:report",
"coverage:remap": "remap-istanbul -i coverage/coverage.json -o coverage/coverage.json -t json",
"coverage:report": "istanbul report html"
},
...
}
回答1:
Install karma-typescript:
npm install karma-typescript --save-dev
Put this in your karma.conf.js:
frameworks: ["jasmine", "karma-typescript"],
files: [
{ pattern: "src/**/*.ts" }
],
preprocessors: {
"**/*.ts": ["karma-typescript"]
},
reporters: ["progress", "karma-typescript"],
browsers: ["Chrome"]
This will run your Typescript unit tests on the fly and generate Istanbul html coverage that look like this:
No need for npm scripts etc, all the magic happens in the plugin.
来源:https://stackoverflow.com/questions/38764929/how-to-do-code-coverage-with-karma-typescript-and-browserify