[英]Can't find source maps for Karma + Jasmine + TypeScript + Webpack
我正在尝试使用Karma,Jasmine和Webpack测试(使用覆盖率)我的TypeScript应用程序。 通过以下内容,我能够成功运行测试,但无法正确生成覆盖。 我正在使用karma-remap-coverage
( https://github.com/sshev/karma-remap-coverage ),看起来很简单。
看起来好像发生了一些有趣的事情(而且我得到了一些报道)但是在这里和那里进行了一些调整,数字变化很大,我实际上永远无法加载源图。
这是基本设置:
我有一个包含10个.ts
文件的src
目录。 目前只有一个具有相应的.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
文件包含在coverage报告中。 由于这是测试文件,我不想包含它。 我觉得我非常接近。 我有什么简单的遗漏或建议吗?
更新:
我意识到我使用的是旧版本的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@0.8.4
remap-istanbul
方面存在问题,但在我使用的版本中没有。
还使用Webpack 2.6.1
和TypeScript 2.3.2
好吧,经过几天尝试不同的事情,我终于找到了一个有效的解决方案。 我不确定在我的第一篇文章中究竟是什么导致了这个问题,但是这里我已经到了最后。 对于寻找非常简单的TypeScript,Karma,Jasmine,Webpack(带覆盖)设置的其他人来说,这可能会有所帮助。
spec
文件保持不变。 我的tsconfig.json
更新为:
{ "compilerOptions": { "module": "commonjs", "target": "es6", "noImplicitAny": false, "inlineSourceMap": true, // this line "sourceMap": false, // and this one "experimentalDecorators": true, "lib": ["es6", "dom"] }, "exclude": [ "node_modules" ] }
我转而使用awesome-typescript-loader
而不是ts-loader
。
最后,我的karma.conf.js
文件现在看起来像:
module.exports = config => config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '', frameworks: ['jasmine'], mime: { 'text/x-typescript': ['ts','tsx'] }, files: [ 'node_modules/angular/angular.min.js', './src/**/*.ts' ], preprocessors: { './src/**/*.ts': ['webpack'] }, webpack: { devtool: 'inline-source-map', module: { rules: [ { enforce: 'pre', test: /\\.js$/, loader: 'source-map-loader', exclude: [ 'node_modules', /\\.spec\\.ts$/ ] }, { test: /\\.ts?$/, use: [ { loader: 'awesome-typescript-loader', query: { /** * Use inline sourcemaps for "karma-remap-coverage" reporter */ sourceMap: false, inlineSourceMap: true, compilerOptions: { removeComments: true } }, } ] }, { enforce: 'post', test: /\\.(js|ts)$/, loader: 'istanbul-instrumenter-loader', exclude: [ /node_modules/, /\\.spec\\.ts$/ ] }, { test: /\\.html$/, loader: 'html-loader' } ] }, resolve: { extensions: [".ts", ".js", ".html"] }, externals: { angular: "angular" } }, 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 });
最终包版本包括:
现在我的测试运行了,控制台中没有错误,我有原始TypeScript文件的覆盖率报告!
很多人都把它放在一起(它最终指导了我的最终解决方案): https : //github.com/AngularClass/angular-starter/tree/master/config
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.