How do you use Istanbul Code Coverage with transpiled Typescript?

后端 未结 5 1186
耶瑟儿~
耶瑟儿~ 2021-02-01 14:11

I\'ve been reading articles on this all morning trying to get my environment setup correctly. But for some reason I\'m not getting it. My setup-

/app
    ... sou         


        
5条回答
  •  执念已碎
    2021-02-01 14:48

    None of the examples provided worked for my Node.JS project (written in TypeScript). I wanted to run unit tests in Jasmine, and covered by Istanbul.

    I ended up getting it working with the following.

    package.json:

    {
      "scripts": {
        "lint": "tslint 'src/**/*.ts'",
        "remap": "./node_modules/.bin/remap-istanbul -i ./coverage/coverage-final.json -t html -o ./coverage && rimraf ./coverage/dist",
        "test": "npm run lint && rimraf dist coverage && tsc --project tsconfig-test.json && ./node_modules/.bin/istanbul cover ./node_modules/.bin/jasmine JASMINE_CONFIG_PATH=jasmine.json && npm run remap"
      },
      "devDependencies": {
        "@types/jasmine": "2.8.6",
        "@types/node": "9.6.6",
        "istanbul": "0.4.5",
        "jasmine": "3.1.0",
        "remap-istanbul": "0.11.1",
        "rimraf": "2.6.2",
        "tslint": "5.9.1",
        "typescript": "2.8.1"
      }
    }
    

    jasmine.json

    {
      "spec_dir": "dist",
      "spec_files": [
        "**/*.spec.js"
      ],
      "stopSpecOnExpectationFailure": false,
      "random": false
    }
    

    .istanbul.yml

    instrumentation:
      root: ./dist
      excludes: ['**/*.spec.js', '**/fixtures/*.js']
      include-all-sources: true
    reporting:
      reports:
        - html
        - json
        - text-summary
      dir: ./coverage
    

    tsconfig-test.json

    {
      "compilerOptions": {
        "declaration": true,
        "lib": [
          "dom",
          "es6"
        ],
        "module": "commonjs",
        "noImplicitAny": true,
        "outDir": "dist",
        "sourceMap": true,
        "target": "es5"
      },
      "include": [
        "src/**/*.ts"
      ],
      "exclude": [
        "node_modules"
      ]
    }
    

提交回复
热议问题