swc-plugin-coverage-instrument icon indicating copy to clipboard operation
swc-plugin-coverage-instrument copied to clipboard

include/exclude files from coverage

Open fullstackzach opened this issue 2 years ago • 10 comments

Hi,

Thanks for providing this plugin, I was wondering if it was possible to include or exclude files for coverage similar to how nyc has the option to here? https://github.com/istanbuljs/nyc#using-include-and-exclude-arrays

I'm trying to get this working with Cypress and Next.js. Currently I have this plugin included but the coverage report includes more files than we would like to report on

in next.config.js

...
  experimental: {
    swcPlugins: [
      ['swc-plugin-coverage-instrument', {}], // used to instrument code for cypress e2e test coverage
    ],
  },
...

fullstackzach avatar Jan 30 '23 23:01 fullstackzach

This plugin attempts to mimic istanbul's api surface as much (https://github.com/istanbuljs/istanbuljs), not the high level api wraps those such as nyc does. If istanbuljs have such interfaces, it can be added. Otherwise, it should be configured by caller instead.

kwonoj avatar Jan 31 '23 00:01 kwonoj

Gotcha thanks. I think that the includes / excludes files feature is a part of nyc & babel-plugin-istanbul.

Unfortunately, this is going to prevent us from switching from babel to SWC with Next.js until we can target coverage instrumentation for only certain files.

fullstackzach avatar Jan 31 '23 01:01 fullstackzach

plus 1 on this request, it'll be amazing if it can support on par files targeting like istanbul/nyc

ivanwidj avatar Jun 15 '23 23:06 ivanwidj

Please do not leave redundant comment.

kwonoj avatar Jun 15 '23 23:06 kwonoj

i meet the same situation, i'm miagrating babel-loader to swc-loader, and istanbul plugin od babel has exclude option which was not worked in swc-plugin-coverage-instrument

GonsonInter avatar Oct 09 '23 15:10 GonsonInter

In .nycrc-config, have you tried using the exclude option with "excludeAfterRemap": true? fullstackzach

timlindsk avatar Oct 13 '23 09:10 timlindsk

In .nycrc-config, have you tried using the exclude option with "excludeAfterRemap": true? fullstackzach

Using excludeAfterRemap fixed issue with node_modules showing. So it's viable for me.

ArnasDickus avatar Dec 02 '23 09:12 ArnasDickus

This prevents me from using Middleware in a Next.js project, because this plugin instruments all files, including middleware.ts. But Middleware uses the edge runtime, instead of nodejs and prohibits eval.

But the instrumented middleware.js in the build output .next/server/src/ includes strings that are evaluated.

Here is a codesandbox with the reproduced error: https://codesandbox.io/p/devbox/swc-instrument-with-middleware-9kw6rq?file=/next.config.js:3,18

With exclude I could exclude middleware.ts from being instrumented.

sebkasanzew avatar Jan 29 '24 13:01 sebkasanzew

Any new here. I have not found a way to use a .nycrc-config in the documentation.

henrikvolmer avatar Feb 21 '24 08:02 henrikvolmer

Hello!

If istanbuljs have such interfaces, it can be added.

If babel-plugin-istanbul have such interface, can it be added to swc-plugin?

https://github.com/istanbuljs/babel-plugin-istanbul?tab=readme-ov-file#ignoring-files

Themezv avatar Mar 11 '24 11:03 Themezv

I'm still not fully convinced if plugin should support this or not, however to see if this things works or not added unstable options to the plugin. unstableExclude will accept an array of glob pattern to be excluded to be instrumented.

https://github.com/kwonoj/swc-plugin-coverage-instrument/blob/4689fc9d281e11c875edd2376e8d92819472b9fe/spec/plugin.spec.ts#L19-L21

kwonoj avatar Jul 04 '24 06:07 kwonoj

Hello @kwonoj, thanks for this update. What should the file look like when it is in the exclude pattern?

I added a middleware file from Next.js to the unstableExclude pattern. But the file looks the same whether I add it to the pattern.

I removed the plugin and compared the compiled version of the middleware with the one with an activated plugin; it looks different.

At first, I thought the pattern was not working. However, after debugging the plugin and compiling a version I tested locally, I can confirm that the pattern matches.

I expect the excluded file matched by the pattern to look like the file when the plugin is inactive. Is that correct?

If so, I will debug more and find a solution.

lhimstedt avatar Jul 17 '24 09:07 lhimstedt

You may refer the test case to see how things work. If the patter matches it should not trigger transform.

kwonoj avatar Jul 17 '24 11:07 kwonoj

@kwonoj I saw the test case but I wanted to double check my hypothesis. I take you comment as a confirmation that the code of the excluded file should look like the file without the plugin. Since it's not the case it must be a bug in Next.js SWC implementation or the library. Let's see.

lhimstedt avatar Jul 17 '24 11:07 lhimstedt

Next-swc's plugin runs same as @swc/core. I do not think there is next.js specific swc bugs.

kwonoj avatar Jul 17 '24 11:07 kwonoj