feat: support istanbul coverage provider#1676
Conversation
|
This is still a work-in-progress but it might be worth to create a draft PR at this point to prevent duplicate work. I think the I think it's also good time for Vitest team to take a look at this overall and call for a stop if this doesn't look good. |
| // TODO: Requring all these packages to be installed by users may be too much | ||
| const requiredPackages = ctx.config.coverage.provider === 'istanbul' | ||
| ? [ | ||
| 'istanbul-lib-coverage', | ||
| 'istanbul-lib-instrument', | ||
| 'istanbul-lib-report', | ||
| 'istanbul-reports', | ||
| ] |
There was a problem hiding this comment.
We can create @vitest/istanbul package that just reexports what is needed.
@vitest-dev/team What do you think?
bc73f74 to
36b3f17
Compare
e657060 to
5b48782
Compare
fea9cce to
fc13750
Compare
| this.ctx = ctx | ||
| this.options = resolveIstanbulOptions(ctx.config.coverage, ctx.config.root) | ||
|
|
||
| const { createInstrumenter } = require('istanbul-lib-instrument') |
There was a problem hiding this comment.
Would be cool to see this configurable to use custom instrumenters like https://github.com/kwonoj/swc-coverage-instrument/
There was a problem hiding this comment.
This seems like a cool idea, thanks for improvement idea @wight554. However I think it might be best if we leave this customization out of this PR and introduce it in a separate feature afterwards.
Do you think there is anything important we need to take into consideration at this point? Leave some abstractions in place, etc?
My Rust knowledge is a bit rusty and to me it's not that clear what exactly import('swc-plugin-coverage-instrument') would return here. Does it match the API of istanbul-lib-instrument?
There was a problem hiding this comment.
This seems like a cool idea, thanks for improvement idea @wight554. However I think it might be best if we leave this customization out of this PR and introduce it in a separate feature afterwards. Do you think there is anything important we need to take into consideration at this point? Leave some abstractions in place, etc? My Rust knowledge is a bit rusty and to me it's not that clear what exactly
import('swc-plugin-coverage-instrument')would return here. Does it match the API ofistanbul-lib-instrument?
I've checked how jest handles this, see https://github.com/facebook/jest/blob/24e0472ac41ea03cdd89ffaea8c5f410ecf6054f/packages/jest-transform/src/ScriptTransformer.ts#L410
In this case all that needs to be added is config value to skip instrumenter part and return unmodified map and code in onFileTransform
|
looks good overall, noticed few issues:
codebase I used for testing: https://github.com/wight554/blog-template Upd. I use typescript instead of esbuild for compiling tests but ts-jest does the same and uses default jest instrument, so shouldn't be an issue |
|
The overall direction looks great to me. Please ping me when you think the work on your side is done, and I can help to do the refactoring and make the packages more general as |
|
From my side all that is left to do is unit+manual testing and add documentation. I think everything else (besides refactoring into separate packages) is done. I'm happy to leave these tasks to other people as well. 😄 I'm currently travelling and won't be doing anything for the next ~10 days. @antfu if you'd like to start working on this now, feel free to take over the PR. 👍 |
|
Great, thanks! Have a good time traveling! |
1bfd7e0 to
15d1ddc
Compare
4c060d9 to
0d28eb2
Compare
|
There's now some initial documentation and unit tests in place. I think the next logical step is to refactor these coverage providers into their own packages - #1676 (comment). |
I think we should have these packages:
Then Vitest checks for |
|
@antfu |
|
It should be noticed |
|
I thought about it as once, but it could be complex to handle, like if you have both c8 and Istanbul packages installed, or if we have more providers in the future. Also, it might be dangerous to have dependencies change the behavior implicitly. |
|
But the current behavior is very strange. When MISSING DEP Can not find dependency '@vitest/coverage-c8'
✖ Do you want to install @vitest/coverage-c8? … noThere is no choice or notice to add When Coverage enabled with istanbulThis message is very redundant to appear every time.
IMO, something like |
Closes #1252
Documentation of the istanbul packages is not perfect. I had to do some analyzing of
jestandnycto get things working.Creates common interface for coverage providers to implement and adds new coverage provider:
onFileTransform:istanbul-lib-instrument. Coverage objects are added toglobalThis.__VITEST_COVERAGE__onBeforeFilesRun:process.env.NODE_V8_COVERAGEonAfterSuiteRun:v8.takeCoverage()globalThis.__VITEST_COVERAGE__from workers through birpc into main threadonAfterAllFilesRun:c8/lib/reportistanbul-lib-coverageand create report withistanbul-reportsExample
vitest/test/coverage-test/src/utils.ts: Istanbul on the left with 57% coverage, v8 on the right with 70% coverage: