Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--all does not include .ts files that use typecasting #679

Closed
nddeluca opened this issue Sep 22, 2017 · 6 comments
Closed

--all does not include .ts files that use typecasting #679

nddeluca opened this issue Sep 22, 2017 · 6 comments

Comments

@nddeluca
Copy link

nddeluca commented Sep 22, 2017

This one stumped me for a while on a typescript code base I added coverage too. I wasn't seeing all my source files being included when using --all, and narrowed it down to files that use the <T> or as T typecasting syntax. Seemed a bit strange, but I was able to set up a new repository and observe the issue in isolation (see below).

Note: This is only when using the -all flag. If one of the files that is affected is included in a test, then it shows up in the coverage report. Also, the typescript generic syntax (ex. function<T>()) does not have this issue either, so I don't believe it's related to the appearance of the brackets.


Expected Behavior

--all should include all .ts files

Observed Behavior

--all does not include .ts files that use typecasting

Bonus Points! Code (or Repository) that Reproduces Issue

See https://github.com/nddeluca/nyc-ts-test.

You can clone, npm install, and npm test to observe the issue.
There are three example .ts files named a, b, and c.

a gets included in the coverage report
b and c both use typecasting and get ignored.

If you edit the code and remove the typecasting, the files will show up in the coverage.

Forensic Information

Operating System: the operating system you observed the issue on.

Darwin Kernel Version 16.3.0

Environment Information: information about your project's environment, see instructions below:

Node 8.4.0
NPM 5.3.0
Typescript 2.4.2

@bcoe bcoe added the triaged label Nov 20, 2017
@bcoe
Copy link
Member

bcoe commented Nov 20, 2017

@nddeluca --all needs to be able to parse all of the files as it walks them. How are you transpiling the source?

If you're using something like typescript-register, make sure that it's nyc that requires typescript-register rather than your test framework.

@Edo78
Copy link

Edo78 commented Apr 20, 2018

I had the vary same problem and solved it moving the require of ts-node/register from mocha to nyc.

Maybe it should be documented or if it's already then it should be easier to find.

@jonaskello
Copy link

I'm using tape with typescript and have this same problem. If I move the --require ts-node/register to nyc instead of tape I get this error: TypeError: Unable to require .d.ts file.

This is the same error as in #504. Actually in that issue, the solution is to move --require ts-node/register from nyc to mocha, and in this issue it the solution is the other way around 😕

@jonaskello
Copy link

I found a solution to my problem above. It is to put --require ts-node/register in nyc and exclude *.d.ts files. So my .nycrc.json now looks like this:

{
  "all": true,
  "include": ["src/*"],
  "exclude": ["src/index.ts", "**/*.d.ts"],
  "extension": [".ts"],
  "reporter": ["html", "lcov", "text"],
  "sourceMap": true,
  "require": ["ts-node/register"]
}

@Aqours
Copy link

Aqours commented Dec 24, 2018

@jonaskello how to include custom types when using nyc mocha ?

global.d.ts file included by tsconfig.json

declare const DEBUG: boolean;

index.ts

if (DEBUG) {
    console.log('debug');
}

It will throw an error error TS2304: Cannot find name 'DEBUG'. when I runing nyc mocha. It seems tsconfig.json not be used by ts-node. But it works fine if I put --require ts-node/register to mocha not nyc.

Solution: Use --require ts-node/register/transpile-only instead of --require ts-node/register

@JaKXz
Copy link
Member

JaKXz commented Jan 5, 2019

I've updated the TS tutorial that had some of the tips in here, with some of the newer tips. If you are able, please contribute to those docs :) https://github.com/istanbuljs/istanbuljs.github.io

@JaKXz JaKXz closed this as completed Jan 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants