istanbuljs / babel-plugin-istanbul

A babel plugin that adds istanbul instrumentation to ES6 code
BSD 3-Clause "New" or "Revised" License
624 stars 73 forks source link

Empty coverage report when using "type": "module" #235

Closed IgorNovozhilov closed 3 years ago

IgorNovozhilov commented 4 years ago

Empty coverage report when using "type": "module" in "package.json".

How to configure everything to work "out of the box"?

Example repo: https://github.com/demo-source/node-app

Here is the output in the original: изображение

Relying on the message: ExperimentalWarning: The ESM module loader is experimental., I assume that the source code is not converted using the module @babel/register, and is still executed as an ESM

But if you manually change the type, but everything works. ("type": "commonjs") if you add a small plug(https://github.com/demo-source/node-app/blob/master/update-package.cjs), and in real-time to replace the type: изображение

coreyfarrell commented 4 years ago

When "type": "module" applies in node.js only *.cjs files are processed by @babel/register. In this situation *.js files are loaded by the node.js ES module loader which does not yet support live source transformations. This is not a limitation of babel, it's a limitation of node.js. Work is being done in node.js to fix this situation but initial support for source transformation hooks is not yet released.

Right now the only options for using nyc involve pre-instrumentation (running babel CLI ahead of time or running nyc instrument), then testing the pre-instrumented code.

IgorNovozhilov commented 4 years ago

@coreyfarrell, Maybe there is a plugin that solves this problem?

coreyfarrell commented 4 years ago

The problem is that node.js does not yet have a transformSource hook for ES modules. There is nothing for a plugin to attach to. node.js 13.7.0 will have this functionality but it's all still experimental.

IgorNovozhilov commented 4 years ago

Well, then I'll use my solution, maybe someone will be useful: https://www.npmjs.com/package/babel-hook-esm-to-cjs-package-type-runtime Or maybe someone will suggest something else...

coreyfarrell commented 4 years ago

Please see https://github.com/istanbuljs/esm-loader-hook. This provides an experimental method for getting coverage of node.js ES modules in nyc. Keep in mind the node.js api's it attaches to are still experimental so the loader hook may break upon release of new versions of node.js.

As an alternative to nyc you might check out https://github.com/bcoe/c8. This option uses native v8 functionality for coverage rather than instrumenting code.

coreyfarrell commented 3 years ago

I'm closing this issue as the linked module provides the required support. This is somewhat outside the scope of babel-plugin-istanbul and more within the scope of test runners. Specifically babel-plugin-istanbul does not inject itself into the module loader (CJS or ESM), that is the responsibility of jest, @babel/register, @istanbuljs/esm-loader-hook or other similar loader hooks.