简体   繁体   English

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

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

I'm trying to test (with coverage) my TypeScript application using Karma, Jasmine, and Webpack. 我正在尝试使用Karma,Jasmine和Webpack测试(使用覆盖率)我的TypeScript应用程序。 With the following, I'm able to successfully run tests, but am unable to generate coverage properly. 通过以下内容,我能够成功运行测试,但无法正确生成覆盖。 I'm using karma-remap-coverage ( https://github.com/sshev/karma-remap-coverage ) and it seems simple enough. 我正在使用karma-remap-coveragehttps://github.com/sshev/karma-remap-coverage ),看起来很简单。

It looks as though something interesting is happening (and I'm getting some kind of coverage report) but with a few tweaks here and there, the numbers change drastically and I can never actually load the sourcemaps. 看起来好像发生了一些有趣的事情(而且我得到了一些报道)但是在这里和那里进行了一些调整,数字变化很大,我实际上永远无法加载源图。

Here's the basic setup: 这是基本设置:

I have a src directory that contains 10 .ts files. 我有一个包含10个.ts文件的src目录。 Only one has a corresponding .spec file at the moment. 目前只有一个具有相应的.spec文件。

The spec file is pretty simple and was just enough to prove that I could run tests: 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();
  });

});

This works like a charm when paired with my tsconfig.json file: 当与我的tsconfig.json文件配对时,这就像一个魅力:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": false,
    "sourceMap": true,
    "lib": ["es6", "dom"],
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules"
  ]
}

and karma.conf.js file: 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

});

And finally, I'm launching the tests with a simple Gulp task: 最后,我用一个简单的Gulp任务启动测试:

gulp.task('test', function (done) {
  new Server({
    configFile: __dirname + '/karma.conf.js',
    singleRun: true
  }, (exitCode) => {
     done();
     process.exit(exitCode);
  }).start();
});

When run, I get an output that seems (mostly) promising: 运行时,我得到一个似乎(大多数)有希望的输出:

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 )
====================================================================

So something is happening! 所以有些事情正在发生! Which makes me feel like I'm close. 这让我觉得我很亲密。 When I browse to my coverage report in a browser, I see both the .spec.ts file and the .ts file listed (which is again, getting closer) but I'm not quite there for a couple of reasons: 当我在浏览器中浏览我的覆盖率报告时,我看到.spec.ts文件和.ts文件都列出了(这又是,越来越近了)但是由于以下几个原因我并不完全在那里:

  1. The .spec.ts file is being included in the coverage report. .spec.ts文件包含在coverage报告中。 Since this is the test file, I do not want to include it. 由于这是测试文件,我不想包含它。
  2. Source maps are not being properly generated - this is clear from the errors in the console and also from the inability to browse to the specific file's coverage report. 源地图未正确生成 - 从控制台中的错误以及无法浏览到特定文件的覆盖率报告中可以清楚地看出这一点。

I do feel like I'm pretty darn close. 我觉得我非常接近。 Is there anything simple that I'm missing or suggestions? 我有什么简单的遗漏或建议吗?

Update: 更新:

I realized I was using an older version of Node and thought that may be causing some issues. 我意识到我使用的是旧版本的Node,并认为这可能会导致一些问题。 I upgraded to 6.11.0 and while that didn't solve anything, it did provide slightly more context: 我升级到6.11.0虽然没有解决任何问题,但确实提供了更多的上下文:

The errors are being reported by remap-istanbul (no surprise there, really): remap-istanbul报告错误(真的没有惊喜):

CoverageTransformer.addFileCoverage (/app/node_modules/remap-istanbul/lib/CoverageTransformer.js:148:17)

I am using karma-remap-coverage@0.1.4 which uses remap-istanbul@0.8.4 - it seems like there have been issues with remap-istanbul in the past, but not at the version I'm using. 我使用的是karma-remap-coverage@0.1.4 ,它使用了remap-istanbul@0.8.4 - 似乎过去在remap-istanbul@0.8.4 remap-istanbul方面存在问题,但在我使用的版本中没有。

Also using Webpack 2.6.1 and TypeScript 2.3.2 还使用Webpack 2.6.1和TypeScript 2.3.2

Well, after several days of trying different things, I've finally found a solution that works. 好吧,经过几天尝试不同的事情,我终于找到了一个有效的解决方案。 I'm not sure specifically what was causing the issue in my first post, but here's where I've ended up. 我不确定在我的第一篇文章中究竟是什么导致了这个问题,但是这里我已经到了最后。 This may be helpful for someone else looking for a really simple TypeScript, Karma, Jasmine, Webpack (with coverage) setup. 对于寻找非常简单的TypeScript,Karma,Jasmine,Webpack(带覆盖)设置的其他人来说,这可能会有所帮助。

  • My file structure and spec file stayed the same. 我的文件结构和spec文件保持不变。
  • My tsconfig.json updated to: 我的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" ] } 
  • I switched to using the awesome-typescript-loader instead of ts-loader . 我转而使用awesome-typescript-loader而不是ts-loader

  • And finally, my karma.conf.js file now looks like: 最后,我的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 }); 

Final package versions include: 最终包版本包括:

  • node 4.2.6 (I was also able to get this to work with a newer version of node, but need to be here for other reasons) 节点4.2.6 (我也能够使用更新版本的节点,但由于其他原因需要在这里)
  • awesome-typescript-loader 3.1.2 awesome-typescript-loader 3.1.2
  • istanbul-instrumenter-loader 2.0.0 istanbul-instrumenter-loader 2.0.0
  • jasmine-core 2.5.2 茉莉核心2.5.2
  • karma 1.6.0 业力1.6.0
  • karma-chrome-launcher 2.0.0 karma-chrome-launcher 2.0.0
  • karma-coverage 1.1.1 业力覆盖1.1.1
  • karma-jasmine 1.1.0 karma-jasmine 1.1.0
  • karma-remap-coverage 0.1.4 karma-remap-coverage 0.1.4
  • karma-webpack 2.0.3 karma-webpack 2.0.3
  • typescript 2.3.2 打字稿2.3.2
  • webpack 2.6.1 webpack 2.6.1

Now my tests run, there are no errors in the console, and I have a coverage report of the original TypeScript files! 现在我的测试运行了,控制台中没有错误,我有原始TypeScript文件的覆盖率报告!

Lots of credit to the folks who put this together (it ended up guiding quite a bit of my final solution): https://github.com/AngularClass/angular-starter/tree/master/config 很多人都把它放在一起(它最终指导了我的最终解决方案): https//github.com/AngularClass/angular-starter/tree/master/config

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

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