Skip to content

Instantly share code, notes, and snippets.

@resal81
Created April 16, 2016 22:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resal81/fed121e8e69d90541201a6e76cdd26b2 to your computer and use it in GitHub Desktop.
Save resal81/fed121e8e69d90541201a6e76cdd26b2 to your computer and use it in GitHub Desktop.
Code coverage for typescript

About

Here are some notes that I gathered when trying to get code coverage for typescript.

Folder structure

proj
|__ package.json
|__ tsconfig.json
|__ src/lib.ts
|__ tests/test1.spec.ts

Needed pacakges:

npm install mocha chai istanbul remap-istanbul typescript

Settings

package.json:

"scripts: {
  "build-ts": "rm -rf build && tsc",
  "test-spec": "mocha build/tests/**/*.spec.js -R spec --bail",
  "test-cov": "istanbul cover --include-all-sources --dir ./build/coverage node_modules/mocha/bin/_mocha -- build/tests/**/*.spec.js -R spec --bail",
  "test-remap": "remap-istanbul -i build/coverage/coverage.json -o coverage -t html",
  "build": "npm run build-ts",
  "test": "npm run build && npm run test-cov"
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": true,
    "jsx": "react",
    "moduleResolution": "node",
    "outDir": "build"
  },
  "exclude": [
    "node_modules",
    "typings"
  ]
}

How to run

npm run test
npm run test-remap

open coverage/index.html

It's good idea to run the tests and remap steps separately so the remap step is run even if tests fail.

Credits

@uisrinivas
Copy link

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment