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

Cannot Import TypeScript Module In Test File #1002

Closed
dawsonc623 opened this issue Feb 26, 2019 · 7 comments
Closed

Cannot Import TypeScript Module In Test File #1002

dawsonc623 opened this issue Feb 26, 2019 · 7 comments

Comments

@dawsonc623
Copy link

Link to bug demonstration repository.

https://github.com/dawsonc623/nyc-bug-demo

Expected Behavior

Running yarn test should yield passing tests with 100% coverage.

Observed Behavior

Running yarn test fails with the following errors and reports 0% Statement and Line coverage (Branch and Funcs are at 100% presumably because there are none in the file).

TSError: ⨯ Unable to compile TypeScript:
src/app/foo.ts(1,212): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
src/app/foo.ts(1,656): error TS2339: Property 'hash' does not exist on type '{ path: string; statementMap: { "0": { start: { line: number; column: number; }; end: { line: number; column: number; }; }; }; fnMap: {}; branchMap: {}; s: { "0": number; }; f: {}; b: {}; _coverageSchema: string; }'.

There is also a massive stack trace, but it does not seem particularly useful to post the whole thing here.

Forensic Information

Operating System: Ubuntu 18.04.2 LTS on Windows 10 via the Windows Subsystem for Linux
Environment Information: https://gist.github.com/dawsonc623/5d175ea45c6c5fb5359dc11bfeb05df3

@ljsustainas
Copy link

ljsustainas commented Mar 7, 2019

use ts-node/register/transpile-only instead
from docs https://istanbul.js.org/docs/tutorials/typescript/

--require ts-node/register # replace with ts-node/register/transpile-only if you have custom types

@dawsonc623
Copy link
Author

@ljsustainas That works, but it seems to disable type-checking when running the tests. Is there not a "best of both worlds" solution? Either way, thanks for the response!

@coreyfarrell
Copy link
Member

ts-node is complaining about two specific statements.

  1. var global new Function("return this")();
  2. coverageData.hash = hash;

Running TS_NODE_IGNORE_DIAGNOSTICS=2339,2351 npm t worked for me without switching to transpile-only. Note this disabled two entire classes of diagnostics so it's not perfect.

We could deal with the TS2339 issue by adding hash to the original declaration of coverageData instead of setting it later. I created istanbuljs/istanbuljs#336 to deal with this issue, just be aware it will take some time before we can make this change.

I have no solution to the new Function error. We need a way to find the global context.

The only other thing I can suggest for now is to have your test run twice. First run without nyc using full ts-node/register, then run with nyc and ts-node/register/transpile-only.

@dawsonc623
Copy link
Author

@coreyfarrell Thanks for digging into this deeper. I will set up my tooling to run the tests twice as you suggest. While not completely solved, it is nice to know the root causes were found.

@coreyfarrell
Copy link
Member

@dawsonc623 istanbuljs/istanbuljs#352 will address the error related to setting coverageData.hash. Once that is merged / released it will be possible to use TS_NODE_IGNORE_DIAGNOSTICS=2351 npm t which would ignore the single issue type. I have opened TypeStrong/ts-node#813 to see if they can help with the error 2351.

@coreyfarrell
Copy link
Member

@dawsonc623 I was actually able to fix both issues you reported here. nyc@14.0.0-alpha.0 has been released to npm to allow easier testing. Please note that additional breaking changes will occur before 14.0.0 final is released. I'm closing this issue, you can monitor release progress at #1051.

@dawsonc623
Copy link
Author

@coreyfarrell Fantastic news! Thanks for tackling this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants