首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用黄瓜覆盖nyc运行时

用黄瓜覆盖nyc运行时
EN

Stack Overflow用户
提问于 2019-02-19 17:02:05
回答 1查看 1.1K关注 0票数 1

我试图获得用黄瓜/TS编写的e2e测试的覆盖率,但是我的测试报告永远无法映射回我的源文件。

该报告只是一个目录列表,单击单个*.ts文件会产生一个堆栈跟踪(***将输出我的用户/回购名称):

代码语言:javascript
复制
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

代码语言:javascript
复制
{
    "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

代码语言:javascript
复制
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 (相关部件):

代码语言:javascript
复制
"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吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-19 21:11:05

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

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

代码语言:javascript
复制
output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index.js',
    devtoolModuleFilenameTemplate: '[resource-path]'}

只有devtool: 'source-map'在webpack.config.js,仅此而已!我们甚至不需要package.json中的package.json配置,也不需要在tsconfig.json中进行任何修改(除了sourceMaps:true )。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54771420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档