简体   繁体   English

Grunt,Istanbul,Isparta和TypeScript

[英]Grunt, Istanbul, Isparta and TypeScript

TL;DR TL; DR

Has anyone used Grunt and Jasmine to successfully generate coverage reports? 有没有人使用Grunt和Jasmine成功生成覆盖率报告?

Long Story: 很长的故事:

I have a few tests, written in Jasmine, for which I wanted to generate some coverage information. 我有一些用Jasmine编写的测试,我想为它生成一些覆盖信息。 For being generic, I have actually used grunt-istanbul, which allows an instrumenter (like isparta) to instrument my code after which a report is generated. 为了通用,我实际上使用了grunt-istanbul,它允许一个指导员(比如isparta)来检测我的代码,然后生成一个报告。 This report, however, is based upon the transpiled code (thus in JavaScript). 但是,此报告基于已转换的代码(因此在JavaScript中)。

I then came across istanbul remap, which looks at the map files generated by the TypeScript compiler and bases the results on that instead; 然后我遇到了istanbul重映射,它查看由TypeScript编译器生成的地图文件,并将结果基于该数据库; however, my instrumenter adds some code to the transpiled code, which means my map files don't match anymore. 但是,我的指导者在转换后的代码中添加了一些代码,这意味着我的地图文件不再匹配了。 At least, this is what I guess as istanbul-remap says: 至少,这是我想的,因为istanbul-remap说:

Error: Could not find source map for: "src/server/modules/service.js" 错误:找不到源映射:“src / server / modules / service.js”

This is part of my grunt configuration: 这是我的grunt配置的一部分:

instrument: {
            files: 'src/server/**/*.js',
            options: {
                lazy: false,
                basePath: '.',
                babel: {
                    sourceMap: true
                },
                instrumenter: require('isparta').Instrumenter
            }
        },
        storeCoverage: {
            options: {
                dir: 'coverage/reports-server/'
            }
        },
        remapIstanbul: {
            dist: {
                options: {
                    reports: {
                        "html": "./coverage/lcov-report",
                        "json": "./coverage/coverage.json"
                    }
                },
                src: "./coverage/reports-server/coverage.json"
            }
        },

actually, I have been able to figure it out myself. 实际上,我已经能够弄明白了。 The trick here goes as follows: 这里的诀窍如下:

  1. compile code with map 用map编译代码
  2. copy code to temporary location 将代码复制到临时位置
  3. instrument code 仪器代码
  4. execute tests 执行测试
  5. copy code back from temporary location 从临时位置复制代码
  6. generate report 生成报告

This may seem clunky but it does the trick! 这可能看起来很笨重,但它确实有效! In my grunt file I have added a copy tasks to automate these temporary location copies for me. 在我的grunt文件中,我添加了一个复制任务,以便为我自动执行这些临时位置副本。

In the end I get to a correct and reliable result. 最后,我得到了正确可靠的结果。

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

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