Even when a ts-file contains only interfaces or types (ignored entities in JavaScript translation), the TypeScript compiler will still create a resulted js-file with one the line:
my-file.ts
interface MyInterface {
someField: string
}
type MyType = string;
This behavior causes a problem with code coverage, especially with the nyc package. The nyc, seeing that lines in a file have never been run (the file has the line of code described above and the file isn't exported anywhere), marks this file as not covered. This problem is described in the issue istanbuljs/nyc#570
To solve this problem in a common way you may remove the Object.defineProperty(exports, "__esModule", { value: true }); line or add the /* istanbul ignore next */ comment before it:
/* istanbul ignore next */
Object.defineProperty(exports, "__esModule", { value: true });
[Solution] ts-node with a custom TypeScript transformer
If you use nyc with ts-node, you may use a custom TypeScript transformer that will find Object.defineProperty(exports, "__esModule", { value: true }); line and add the comment before it.
Create a file with this code (gist link) and include it instead of ts-node in the nyc config:
.nycrc
...
"require": [
- "ts-node/register"
+ "<the path to this file>"
],
...
TL;DR: The solution for the issue is at the end of this post
Generally, when TypeScript uses the CommonJs as a module system, the TypeScript compiler generates the following line in each js-file:
Even when a ts-file contains only interfaces or types (ignored entities in JavaScript translation), the TypeScript compiler will still create a resulted js-file with one the line:
my-file.ts
my-file.js
This behavior causes a problem with code coverage, especially with the nyc package. The nyc, seeing that lines in a file have never been run (the file has the line of code described above and the file isn't exported anywhere), marks this file as not covered. This problem is described in the issue istanbuljs/nyc#570
To solve this problem in a common way you may remove the
Object.defineProperty(exports, "__esModule", { value: true });
line or add the/* istanbul ignore next */
comment before it:[Solution] ts-node with a custom TypeScript transformer
If you use nyc with ts-node, you may use a custom TypeScript transformer that will find
Object.defineProperty(exports, "__esModule", { value: true });
line and add the comment before it. Create a file with this code (gist link) and include it instead of ts-node in the nyc config:.nycrc