我正在尝试使用 Karma、Jasmine 和 Webpack 测试(覆盖)我的 TypeScript 应用程序。使用以下内容,我能够成功运行测试,但无法正确生成覆盖率。我正在使用karma-remap-coverage
(https://github.com/sshev/karma-remap-coverage),它看起来很简单。
看起来好像发生了一些有趣的事情(我得到了某种覆盖率报告),但是在这里和那里进行了一些调整,数字发生了巨大的变化,我永远无法真正加载源地图。
这是基本设置:
我有一个src
包含 10 个.ts
文件的目录。目前只有一个有相应的.spec
文件。
该spec
文件非常简单,足以证明我可以运行测试:
import ComponentToTest from './componentToTest';
describe('ComponentToTest', () => {
it('should run a test', () => {
expect(1+1).toBe(2);
});
it('should be able to invoke the a method', () => {
spyOn(ComponentToTest, 'foo').and.callThrough();
ComponentToTest.foo('testing foo');
expect(ComponentToTest.foo).toHaveBeenCalled();
});
});
tsconfig.json
与我的文件配对时,这就像一个魅力:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": true,
"lib": ["es6", "dom"],
"experimentalDecorators": true
},
"exclude": [
"node_modules"
]
}
和karma.conf.js
文件:
module.exports = config => config.set({
frameworks: ['jasmine'],
mime: { 'text/x-typescript': ['ts','tsx'] },
// if I make this a generic './src/**/*.ts' it seems to freeze up
// without throwing any errors or running any tests, but that seems
// like a separate issue...
files: [
'./src/lib/componentToTest.ts',
'./src/lib/componentToTest.spec.ts'
],
preprocessors: {
'./src/**/*.spec.ts': ['webpack'],
'./src/**/*.ts': ['webpack', 'sourcemap', 'coverage']
},
webpack: {
devtool: "source-map",
module: {
rules: [
{
test: /\.ts?$/,
loader: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: [".ts", ".js"]
}
},
webpackMiddleware: {
quiet: true,
stats: {
colors: true
}
},
// add both "karma-coverage" and "karma-remap-coverage" reporters
reporters: ['progress', 'coverage', 'remap-coverage'],
// save interim raw coverage report in memory
coverageReporter: {
type: 'in-memory'
},
// define where to save final remaped coverage reports
remapCoverageReporter: {
'text-summary': null,
html: './coverage/html',
cobertura: './coverage/cobertura.xml'
},
colors: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
最后,我用一个简单的 Gulp 任务启动测试:
gulp.task('test', function (done) {
new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, (exitCode) => {
done();
process.exit(exitCode);
}).start();
});
运行时,我得到一个看起来(大部分)有希望的输出:
Chrome 58.0.3029 (Mac OS X 10.12.3): Executed 1 of 2 SUCCESS (0 secs / 0.002 secs)
Chrome 58.0.3029 (Mac OS X 10.12.3): Executed 2 of 2 SUCCESS (0.026 secs / 0.004 secs)
[Error: Could not find source map for: "app/src/lib/componentToTest.ts"]
[Error: Could not find source map for: "app/src/lib/componentToTest.spec.ts"]
========================= Coverage summary =========================
Statements : 43.69% ( 322/737 )
Branches : 15.7% ( 38/242 )
Functions : 35.47% ( 61/172 )
Lines : 44.91% ( 322/717 )
====================================================================
所以有些事情正在发生!这让我觉得我很亲近。当我在浏览器中浏览到我的覆盖率报告时,我看到了.spec.ts
文件和.ts
列出的文件(这又是越来越近了),但由于以下几个原因,我并不在那里:
- 该
.spec.ts
文件包含在覆盖率报告中。由于这是测试文件,我不想包含它。 - 未正确生成源映射 - 从控制台中的错误以及无法浏览到特定文件的覆盖率报告中可以清楚地看出这一点。
我确实觉得我非常接近。我有什么简单的遗漏或建议吗?
更新:
我意识到我使用的是旧版本的 Node,并认为这可能会导致一些问题。我升级到6.11.0
虽然没有解决任何问题,但它确实提供了更多的上下文:
错误正在报告remap-istanbul
(这并不奇怪,真的):
CoverageTransformer.addFileCoverage (/app/node_modules/remap-istanbul/lib/CoverageTransformer.js:148:17)
我正在使用karma-remap-coverage@0.1.4
哪些用途remap-istanbul@0.8.4
- 过去似乎存在问题remap-istanbul
,但在我使用的版本中没有。
也使用 Webpack2.6.1
和 TypeScript2.3.2