New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UserConfigExport['coverage']['provider']
with a object option did not work at all
#2797
Comments
UserConfigExport['coverage']['provider']
with a object option did not work at all
Custom coverage providers have been broken for a while. 🙃
In addition to this bug there is another issue where either Tinypool or brpc throws |
It will throw this error if |
@mmis1000 following seems to work on the reproduction case: import { defineConfig } from 'vitest/config';
import { CoverageProvider, CoverageProviderModule } from 'vitest';
class CoverageModule implements CoverageProviderModule {
getProvider(): CoverageProvider {
throw 'called';
}
};
export default defineConfig({
test: {
coverage: {
enabled: true,
provider: new CoverageModule(),
},
},
});
I'm not sure when exactly this has been fixed - by accident. I've had some tests for custom reporters in separate feature branch and earlier those were not working at all. Now they seem to run nicely. I'll set up PR for those tests later. Here's some draft for reference: custom-coverage-provider.tsimport type { CoverageProvider, CoverageProviderModule, ResolvedCoverageOptions, Vitest } from 'vitest'
class CustomCoverageProvider implements CoverageProvider {
name = 'custom-coverage-provider'
ctx!: Vitest
options!: ResolvedCoverageOptions
initialize(ctx: Vitest) {
this.ctx = ctx
this.options = {} as ResolvedCoverageOptions
}
clean() {
console.log('CustomProvider::clean')
}
onBeforeFilesRun() {
console.log('CustomProvider::onBeforeFilesRun')
}
onAfterSuiteRun() {
console.log('CustomProvider::onAfterSuiteRun')
}
reportCoverage() {
console.log('CustomProvider::reportCoverage')
}
onFileTransform() {
console.log('CustomProvider::onFileTransform')
}
resolveOptions() {
return this.options
}
}
export default class CustomCoverageProviderModule implements CoverageProviderModule {
getProvider(): CoverageProvider {
return new CustomCoverageProvider()
}
takeCoverage() {}
} vitest.config.tsimport { defineConfig } from 'vitest/config'
import CustomCoverageProviderModule from './custom-coverage-provider'
export default defineConfig({
test: {
coverage: {
enabled: true,
provider: new CustomCoverageProviderModule(),
},
},
}) $ pnpm test
CustomProvider::clean
RUN v0.28.4 /Users/x/y/vitest/test/coverage-test
Coverage enabled with custom-coverage-provider
CustomProvider::onBeforeFilesRun
CustomProvider::onFileTransform
CustomProvider::onFileTransform
...
Test Files 3 passed (3)
Tests 7 passed (7)
Start at 12:03:47
Duration 410ms (transform 159ms, setup 15ms, collect 133ms, tests 20ms)
% Coverage report from custom-coverage-provider
CustomProvider::reportCoverage |
It seems to work because the Thus it does not trigger the buggy path in The config has a default value of |
Well, there still seems to be a problem when custom provider is used in worker. vitest/packages/vitest/src/integrations/coverage.ts Lines 29 to 34 in aa1aa4d
Is there any other option than to change the API for custom providers to be a path where the provider can be loaded from? Then it could be loaded and initialized inside worker. |
I am not sure if the Another idea would be use the |
If you need to use it inside a worker, it should be a path to the module that will be imported inside. Beware that it will always be two different instances. |
Inside workers we can safely create/import instances of CoverageProviderModules. The
Currently Vitest supports regular test reporters to be written in Typescript, loaded here: vitest/packages/vitest/src/node/reporters/utils.ts Lines 8 to 11 in aa1aa4d
Now that CoverageProviderModule needs to be imported inside worker while running in Edit: Looks like we can use |
Describe the bug
When you defined setting as following
It always say your provider is
c8
Note: it is a bug in
vitest/packages/vitest/src/utils/base.ts
Lines 93 to 98 in 482b72f
Condition here do no check for
isMergeableObject(target[key])
.With in this case, cause
deepMerge({ provider: 'c8' }, { provider: CoverageModule })
to result in{ provider: 'c8' }
Yet another note:
It would still crash even this fixed because it is unable to send this object to other worker.
This option should probably not be the module instance at first place.
Yet Yet another note:
It seems worker is used even I run it with
vitest run --no-threads --coverage --no-isolate
, this can not be right...Reproduction
Run
npx vitest --run --coverage
in https://stackblitz.com/edit/vitest-dev-vitest-x4h2tg?file=vite.config.tsSystem Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: