2

I have a project with two typescript files: - src/index.ts - src/index.spec.ts

I can run the mocha unit tests without first compiling the typescript to commonjs format like this:

mocha -r ts-node/register src/**/*.spec.ts

I'd also like to run istantbul and I tried this:

istanbul cover node_modules/mocha/bin/_mocha -r ts-node/register src/**/*.spec.ts - - -R spec

I got that snippet from this article (It's tweaked to support ts-node.

When istanbul cover is ran like that the result is:

    > @fsngx/ease@1.0.0 cover /home/ole/fsngx/ease
    > istanbul cover node_modules/mocha/bin/_mocha -r ts-node/register src/**/*.spec.ts - - -R spec

    Warning: Could not find any test files matching pattern: ts-node/register
    Warning: Could not find any test files matching pattern: -

Is it possible to run istanbul cover using ts-node or do we need to run that command on compiled typescript?

Ole
  • 41,793
  • 59
  • 191
  • 359
  • To run istanbul - you need to add a dedicated script to package.json. E.g.: `"coverage": "nyc --reporter=html --reporter=text mocha"`, also - you seem to be missing `"require": [ "ts-node/register" ]` and `include: ["./YOUR_ACTUAL_PATH/**/*.spec.ts"]`. Consider checking the official tutorial https://istanbul.js.org/docs/tutorials/typescript/ – c69 Mar 20 '18 at 03:49
  • see my config for mocha/nyc (i do not claim its the most eficient, though, but it works and generates coverage) - https://github.com/c69/hs-sim/blob/master/package.json and https://github.com/c69/hs-sim/blob/master/test/mocha.opts – c69 Mar 20 '18 at 03:53
  • 1
    @c69 thanks - I did attempt to implement the approach used in https://istanbul.js.org/docs/tutorials/typescript/ and updated the repository correspondingly. It does not look like my package.json script is picking up mocha.opts. Added another more detailed post here: https://stackoverflow.com/questions/49374421/adding-support-for-istanbul-test-coverage-for-mocha-unit-tests-written-in-typesc – Ole Mar 20 '18 at 04:26

0 Answers0