question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot Import TypeScript Module In Test File

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
ljsustainascommented, 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
0reactions
dawsonc623commented, Apr 3, 2019

@coreyfarrell Fantastic news! Thanks for tackling this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest cannot import TypeScript file - javascript
Test suite failed to run Jest encountered an unexpected token This usually means that you are trying to import a file which Jest...
Read more >
TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files...
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
TypeScript: Documentation - Modules
In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without...
Read more >
Configuring Jest
To read TypeScript configuration files Jest requires ts-node . ... This option tells Jest that all imported modules in your tests should be ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found