繁体   English   中英

无法找到Karma + Jasmine + TypeScript + Webpack的源地图

[英]Can't find source maps for Karma + Jasmine + TypeScript + Webpack

我正在尝试使用Karma,Jasmine和Webpack测试(使用覆盖率)我的TypeScript应用程序。 通过以下内容,我能够成功运行测试,但无法正确生成覆盖。 我正在使用karma-remap-coveragehttps://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文件都列出了(这又是,越来越近了)但是由于以下几个原因我并不完全在那里:

  1. .spec.ts文件包含在coverage报告中。 由于这是测试文件,我不想包含它。
  2. 源地图未正确生成 - 从控制台中的错误以及无法浏览到特定文件的覆盖率报告中可以清楚地看出这一点。

我觉得我非常接近。 我有什么简单的遗漏或建议吗?

更新:

我意识到我使用的是旧版本的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 }); 

最终包版本包括:

  • 节点4.2.6 (我也能够使用更新版本的节点,但由于其他原因需要在这里)
  • awesome-typescript-loader 3.1.2
  • istanbul-instrumenter-loader 2.0.0
  • 茉莉核心2.5.2
  • 业力1.6.0
  • karma-chrome-launcher 2.0.0
  • 业力覆盖1.1.1
  • karma-jasmine 1.1.0
  • karma-remap-coverage 0.1.4
  • karma-webpack 2.0.3
  • 打字稿2.3.2
  • webpack 2.6.1

现在我的测试运行了,控制台中没有错误,我有原始TypeScript文件的覆盖率报告!

很多人都把它放在一起(它最终指导了我的最终解决方案): https//github.com/AngularClass/angular-starter/tree/master/config

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM