1

I'm building a project that uses mocha-webpack to test an Angular 2 project, and I'm trying to add coverage to the project using Istanbul's command line (nyc), but I can't figure out how to get Istanbul to correctly display the covered source when clicking through the html report. Is there something I'm missing in my configuration? Do I need to use Babel/Karma in order to correctly map these?

webpack.js

var webpack = require('webpack');
var helpers = require('./helpers');

module.exports = {
  target: "node",
  devtool: 'inline-source-map',

  entry: {
    'test': 'mocha-loader!./config/mocha-test-shim.js'
  },

  resolve: {
    modules: [helpers.root('app'), "node_modules"],
    extensions: ['', '.ts', '.js']
  },

  output: {
    devtoolModuleFilenameTemplate: '[absolute-resource-path]',
    devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]'
  },

  module: {
    postLoaders: [{
      test: /\.(js|ts)/,
      exclude: /(node_modules|bower_components)/,
      include: helpers.root('app'),  // instrument only testing sources with Istanbul, after other loaders run
      loader: 'istanbul-instrumenter-loader'
    }],
    loaders: [
      {
        test: /\.ts$/,
        loaders: ['awesome-typescript-loader', 'angular2-template-loader']
      }
    ]
  }
}

package.json (pieces omitted for brevity)

{
  "scripts": {
    "coverage": "nyc --reporter=html mocha-webpack",
  },
  "nyc": {
    "include": [
      "app/**/*.ts",
      "app/**/*.js"
    ],
    "sourceMap": false,
    "instrument": false
  }
}

mocha-webpack.opts

--webpack-config config/webpack.mocha.js
--require config/mocha-test-shim.js
app/**/*.test.ts

With this configuration, I get an html coverage report, but when I click into one of the classes, I get the following error:

Unable to lookup source: /<project-path>/node_modules/angular2-template-loader/index.js!/<project-path>/app/customer/customer.ts(ENOENT: no such file or directory, open '/<project-path>/node_modules/angular2-template-loader/index.js!/<project-path>/app/customer/customer.ts')
Error: Unable to lookup source: /<project-path>/node_modules/angular2-template-loader/index.js!/<project-path>/app/customer/customer.ts(ENOENT: no such file or directory, open '/<project-path>/node_modules/angular2-template-loader/index.js!/<project-path>/app/customer/customer.ts')
    at Context.defaultSourceLookup [as sourceFinder] (/<project-path>/node_modules/nyc/node_modules/istanbul-lib-report/lib/context.js:15:15)
    at Context.getSource (/<project-path>/node_modules/nyc/node_modules/istanbul-lib-report/lib/context.js:74:17)
    at Object.annotateSourceCode (/<project-path>/node_modules/nyc/node_modules/istanbul-reports/lib/html/annotator.js:175:38)
    at HtmlReport.onDetail (/<project-path>/node_modules/nyc/node_modules/istanbul-reports/lib/html/index.js:217:39)
    at Visitor.(anonymous function) [as onDetail] (/<project-path>/node_modules/nyc/node_modules/istanbul-lib-report/lib/tree.js:34:30)
    at ReportNode.Node.visit (/<project-path>/DSW/node_modules/nyc/node_modules/istanbul-lib-report/lib/tree.js:123:17)
    at /<project-path>/DSW/node_modules/nyc/node_modules/istanbul-lib-report/lib/tree.js:116:23
    at Array.forEach (native)
    at visitChildren (/<project-path>/DSW/node_modules/nyc/node_modules/istanbul-lib-report/lib/tree.js:115:32)
    at ReportNode.Node.visit (/<project-path>/DSW/node_modules/nyc/node_modules/istanbul-lib-report/lib/tree.js:126:5)
4

1 回答 1

1

I had the same problem, I resolved by in-lining the source maps using 'awesome-typescript-loader'.

Here is my conf :

{
    test: /\.ts$/,
    include: helpers.root('src'),
    exclude: [/\.e2e\.ts$/],
    loaders: [{
        loader: 'awesome-typescript-loader',
        query: {
            // use inline sourcemaps for "coverage" reporter
            sourceMap: false,
            inlineSourceMap: true,
            compilerOptions: {
                // Remove TypeScript helpers to be injected
                // below by DefinePlugin
                removeComments: true
            }
        }
    }, 'angular2-template-loader']
}

Hop it can help.

于 2017-04-20T10:21:53.290 回答