swc-plugin-coverage-instrument
swc-plugin-coverage-instrument copied to clipboard
include/exclude files from coverage
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
],
},
...
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.
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.
plus 1 on this request, it'll be amazing if it can support on par files targeting like istanbul/nyc
Please do not leave redundant comment.
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
In .nycrc-config, have you tried using the exclude option with "excludeAfterRemap": true? fullstackzach
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.
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.
Any new here. I have not found a way to use a .nycrc-config in the documentation.
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
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
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.
You may refer the test case to see how things work. If the patter matches it should not trigger transform.
@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.
Next-swc's plugin runs same as @swc/core. I do not think there is next.js specific swc bugs.