nyc icon indicating copy to clipboard operation
nyc copied to clipboard

[HELP!] Coverage with source-maps for pre-compiled typescript code

Open epoberezkin opened this issue 4 years ago • 6 comments

I am running typescript mocha tests for pre-compiled typescript code (that is there are both typescript and javascript tests, they require from dist, not from src).

The reason to do it this way is because tsconfig for the project is different from tsconfig for the tests - there are several reasons to keep it this way...

I tried:

  • --exclude-after-remap=false (it is mentioned here: https://www.npmjs.com/package/nyc#source-map-support-for-pre-instrumented-codebases)
  • @istanbuljs/nyc-config-typescript
  • sourceMap: true/false

package.json excerpt:

{
  "scripts": {
    "test-spec": "TS_NODE_PROJECT=spec/tsconfig.json mocha -r source-map-support/register -r ts-node/register spec/{**/,}*.spec.{js,ts} -R dot",
    "test-cov": "nyc --exclude-after-remap=false npm run test-spec"
  },
  "nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "exclude": [
      "**/spec/**",
      "node_modules"
    ],
    "require": [
      "ts-node/register"
    ],
    "reporter": [
      "lcov",
      "text-summary",
      "html"
    ],
    "sourceMap": true
  }
}

If I configure it to include only typescript source files, then the coverage report is empty.

There should be some way to configure it to use source maps in dist folder, but I cannot figure it out - I tried different combinations of options but they all seem to assume that I import typescript source files from mocha tests...

Thank you!

epoberezkin avatar Sep 07 '20 14:09 epoberezkin

Just to add, if mocha tests are pure JS, istanbul shows coverage using available source maps:

"test-spec": "mocha spec/{**/,}*.spec.js -R dot"

So maybe the problem is to make mocha or ts-node to take these source maps into account and to pass them through?

epoberezkin avatar Sep 07 '20 14:09 epoberezkin

@epoberezkin Did you find a working solution?

LeoKotschenreuther avatar Aug 09 '21 13:08 LeoKotschenreuther

I'm having the same problem, tried with sourceMaps: true, inlineSourceMaps: true for tsconfig, tried adding -r source-map-support/register to both nyc or node process. None of the combinations seem to work. I alwasy get coverage for the dist files.

Reproduction added in https://github.com/istanbuljs/nyc/issues/1448

rohit-gohri avatar Mar 03 '22 05:03 rohit-gohri

Was this ever resolved? Looking to also generate typescript source maps

stateofkate avatar Feb 21 '24 01:02 stateofkate

Full disclosure: I haven't spent the time to really understand the problem nor its root cause, but I've managed to get instrumentation working on JS output from the TypeScript, including proper resolution of the source-code filenames from source-maps. My setup is a bit special, since I have a mono-repo structure with tests defined outside of my library package, but I believe this should work equally well if ran when tests are co-located with the test files:

  • I cannot get it working if setting all: true, so this must be false or left out entirely.
  • I need to include both:
    • dist/** (containing .js, .d.ts and .js.map files) and
    • src/** (containing .ts files)
  • Might or might not be relevant to know that all code in packages/my-lib/dist use CommonJS modules - I haven't tested with ESM.

This is my current configuration:

// integration-tests/tests/.nycrc.json
{
  "cwd": "../..",
  "include": [
    "packages/my-lib/dist/**",
    "packages/my-lib/src/**"
  ]
}

Notice that I use cwd to navigate directory up to allow the package/my-lib to be included relative to the root of my repo.

@rohit-gohri I managed to fix your reproduction, by deleting the all: true from the .nycrc file.

Admittedly, all of the above are workarounds and not the behavior I'd expect from nyc.

kraenhansen avatar Feb 22 '24 12:02 kraenhansen