黄瓜的NYC运行时覆盖率

时间:2019-02-19 17:02:06

标签: typescript istanbul nyc

我正在尝试覆盖以黄瓜/ TS编写的e2e测试,但是我的测试报告永远无法映射回我的源文件。

该报告只是目录列表,单击进入单个* .ts文件会产生一个堆栈跟踪({***列出了我的用户/存储库名称):

Unable to lookup source: /Users/***/dev/git/***/dist/webpack:/src/gql/index.ts(ENOENT: no such file or directory, open '/Users/***/dev/git/***/dist/webpack:/src/gql/index.ts')
Error: Unable to lookup source: /Users/***/dev/git/***/dist/webpack:/src/gql/index.ts(ENOENT: no such file or directory, open '/Users/***/dev/git/***/dist/webpack:/src/gql/index.ts')

我要做什么

  1. 使用TypeScript源映射运行检测的应用程序
  2. 对此应用程序从外部运行测试
  3. 停止应用程序并生成原始TS代码的覆盖率报告

我的设置如下

  • 节点:v9.4.0
  • webpack:^4.23.0,
  • 打字稿:~3.1.6

tsconfig

{
    "compilerOptions": {
    "rootDir": "./src",
    "module": "commonjs",
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "noEmitOnError": true,
    "declaration": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "lib": ["esnext"],
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "src",
    "noImplicitAny": true,
    "typeRoots": ["./typings", "./node_modules/@types"],
    "experimentalDecorators": true
  },
  "exclude": ["node_modules", "dist"]
}

webpack.config.js

module.exports = {
  entry: ['./src/index.ts'],
  target: 'node',
  mode: process.env.NODE_ENV === 'dev' ? 'development' : 'production',
  externals: [nodeExternals()], // in order to ignore all modules in node_modules folder,
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    modules: [path.resolve(__dirname, 'src'), 'node_modules']
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index.js'
  }
}

package.json (相关部分):

"scripts": {
    "start-coverage": "NODE_ENV=test nyc node --reporter=lcov dist/index.js",
    ...
}, 
"nyc": {
  "all": true,
  "require": "ts-node/register",
  "instrument": true,
  "report-dir": "./coverage",
  "temp-dir": "./temp",
  "source-map": true,
  "produce-source-map": false
},

问题仍然是以上显示的堆栈跟踪无法映射单个文件。我尝试了许多配置组合,并总是在这里结束。

  • 有人知道怎么了吗?
  • 我应该为此使用remap-istanbul吗?

1 个答案:

答案 0 :(得分:1)

发现了问题。这是webpack的一个错误,已在https://github.com/SitePen/remap-istanbul/issues/68中修复。

解决方案在webpack中,您需要添加选项: devtoolModuleFilenameTemplate。输出部分如下所示:

output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index.js',
    devtoolModuleFilenameTemplate: '[resource-path]'}

在webpack.config.js中只有devtool: 'source-map',就是这样!我们甚至不需要nyc中的package.json配置,也不需要tsconfig.json中的sourceMaps:true以外的任何修改。

相关问题