Skip to content
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

How should I configure my custom reporter in Vitest UI? #7354

Open
4 tasks done
zhangtao25 opened this issue Jan 24, 2025 · 3 comments
Open
4 tasks done

How should I configure my custom reporter in Vitest UI? #7354

zhangtao25 opened this issue Jan 24, 2025 · 3 comments

Comments

@zhangtao25
Copy link

Clear and concise description of the problem

This is our custom HTML reporter, and I want to use it in the coverage section of Vitest UI.

Image

Suggested solution

It is recommended to configure the coverage reporter for coverage reports.

Alternative

No response

Additional context

No response

Validations

@AriPerkkio
Copy link
Member

This is not possible. The html reporter is hard-coded here:

function resolveCoverageFolder(ctx: Vitest) {
const options = ctx.config
const htmlReporter
= options.api?.port && options.coverage?.enabled
? toArray(options.coverage.reporter).find((reporter) => {
if (typeof reporter === 'string') {
return reporter === 'html'
}
return reporter[0] === 'html'
})
: undefined
if (!htmlReporter) {
return undefined
}
// reportsDirectory not resolved yet
const root = resolve(
ctx.config?.root || options.root || process.cwd(),
options.coverage.reportsDirectory || coverageConfigDefaults.reportsDirectory,
)
const subdir
= Array.isArray(htmlReporter)
&& htmlReporter.length > 1
&& 'subdir' in htmlReporter[1]
? htmlReporter[1].subdir
: undefined
if (!subdir || typeof subdir !== 'string') {
return [root, `/${basename(root)}/`]
}
return [resolve(root, subdir), `/${basename(root)}/${subdir}/`]
}

const coverageFolder = resolveCoverageFolder(ctx)
const coveragePath = coverageFolder ? coverageFolder[1] : undefined
if (coveragePath && base === coveragePath) {
throw new Error(
`The ui base path and the coverage path cannot be the same: ${base}, change coverage.reportsDirectory`,
)
}
if (coverageFolder) {
server.middlewares.use(
coveragePath!,
sirv(coverageFolder[0], {
single: true,
dev: true,
setHeaders: (res) => {
res.setHeader(
'Cache-Control',
'public,max-age=0,must-revalidate',
)
},
}),
)
}

@AriPerkkio AriPerkkio added the feat: ui Vitest UI label Jan 24, 2025
@zhangtao25
Copy link
Author

When configuring an HTML reporter, if I also configure my own custom HTML reporter, it can overwrite the previous HTML report.

https://github.com/canyon-project/canyon-test/blob/main/vitest.config.ts#L10

Image

@AriPerkkio
Copy link
Member

That actually works by accident - the reporters are looped in the order they are defined in configuration. If you are saving the reports in same directory as built-in html does, it will just overwrite the results.

for (const reporter of this.options.reporter) {
// Type assertion required for custom reporters
reports
.create(reporter[0] as Parameters<typeof reports.create>[0], {
skipFull: this.options.skipFull,
projectRoot: this.ctx.config.root,
...reporter[1],
})
.execute(context)
}

I think we should have a better solution for this. Maybe something like

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    ui: {
      coverageDirectory: "/somepath"
    }
  },
});

... and if that's defined, use it instead of trying to resolve the path from coverage options.
The current resolving logic should be used when coverageDirectory is not defined for backwards compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants