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

feat(ui): add dark theme to istanbul html reporter #6281

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

userquin
Copy link
Member

@userquin userquin commented Aug 5, 2024

Description

This PR adds custom dark theme css for istambul html reporter modified from this https://userstyles.world/style/14631/istanbul-lcov-material-theme-darker

Check also istanbuljs/istanbuljs#553 (comment)

I need to override the css files when using standalone, rn only works when running Vitest UI (shouldn't work when running npx serve coverage)

imagen
index with dark theme

imagen
index with light theme

imagen
detail with dark theme

imagen
detail with light theme

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Aug 5, 2024

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit c7d8895
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/66b125ffb806f40008b845de
😎 Deploy Preview https://deploy-preview-6281--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@userquin
Copy link
Member Author

userquin commented Aug 5, 2024

Coverage including html folder when running standalone report (--reporter=html), no idea what's happening.

Added onFinishedReportCoverage to the html reporter to allow copy/paste custom css and dark script to html pages after coverage finish (update vitest core to call it), cannot use onFinished since the coverage run after it.

The html reporter not being resolved from local (added the reporter subpackage to root tsconfig base with no luck).

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making this UI specific, could we jus create a custom coverage reporter that extends Istanbul's html reporter with theme toggle? And when ever user defines html reporter, we use that custom built-in one instead?

@sheremet-va sheremet-va changed the title feat(ui): add dark theme to istambul html reporter feat(ui): add dark theme to istanbul html reporter Aug 6, 2024
@userquin
Copy link
Member Author

userquin commented Aug 6, 2024

Html reporter is the custom reporter you are asking for. Vitest will check for html reporter in the configuration to include the reporter from the ui package: check coverage docs (for example, will not work with html-spa reporter).

This reporter is only used with Vitest UI or Browser Mode and will work only with the istambul html reporter (both istambul and v8 using this reporter).

IIRC Vlad asked in discord about changing the reporter name to web.

packages/ui/package.json Outdated Show resolved Hide resolved
packages/ui/node/utils.ts Outdated Show resolved Hide resolved
@AriPerkkio
Copy link
Member

Html reporter is the custom reporter you are asking for. Vitest will check for html reporter in the configuration to include the reporter from the ui package: check coverage docs (for example, will not work with html-spa reporter).

This reporter is only used with Vitest UI or Browser Mode and will work only with the istambul html reporter (both istambul and v8 using this reporter).

Sorry I'm not sure I'm following here. Istanbul's html reporter is used to generate the report that Vitest UI shows. This PR wants to add custom CSS/JS for that report.

Why does this need to be Vitest UI specific only?

IIRC Vlad asked in discord about changing the reporter name to web.

Sure, we can do that. For the public API we would require users to specify web reporter, and then internally convert that to Istanbul's html reporter.

@userquin
Copy link
Member Author

userquin commented Aug 6, 2024

Sorry I'm not sure I'm following here. Istanbul's html reporter is used to generate the report that Vitest UI shows. This PR wants to add custom CSS/JS for that report.

Vitest UI will show the coverage link in the UI only when html reporter added to the configuration and coverage.enabled flag enabled. This PR changes the css styles and the html pages generated by Istanbul html reporter to support dark theme inside Vitest UI (rn, when switching Vitest UI to dark, the html from the coverage folder doesn't support dark theme: Vitest UI adding iframe for coverage via coverage middleware).

This PR is about integrating Istanbul html reporter in Vitest UI: it is out of scope how can we integrate dark theme outside Vitest UI. For example, some users may want to use system theme, others may want dark or light only (with a custom color palette like https://userstyles.world/style/14631/istanbul-lcov-material-theme-darker).

Vitest UI using vueuse to switch between dark and light themes, this PR adds the corresponding script to all html pages to access and watch the corresponding local storage theme key and updates the css files to match Vitest UI color palette.

Check linked Istanbul issue, there is a PR to add system theme to the html reporter.

NOTE: the Vitest UI html reporter also generating standalone output when enabling it via --reporter=html (check last tip in the docs: https://vitest.dev/guide/ui.html#vitest-ui).

@userquin userquin marked this pull request as ready for review August 6, 2024 15:52
@@ -1097,6 +1097,9 @@ export class Vitest {
if (reporter instanceof WebSocketReporter) {
reporter.onFinishedReportCoverage()
}
else if ('onFinishedReportCoverage' in reporter && typeof reporter.onFinishedReportCoverage === 'function') {
await reporter.onFinishedReportCoverage()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we find a better way to do this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to check reporter instanceof HTMLReporter just like WebSocketReporter above?

In #5242 (comment), we discussed and we didn't go with onFinishedReportCoverage at interface Reporter level for now (though it might change in reporter API rework #6147).

Copy link
Member

@sheremet-va sheremet-va Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTMLReporter is defined in @vitest/ui, not in vitest, so I don't think we can do that. Maybe we can add a special property 🤷🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a new type like BuildReporter?

async onFinishedReportCoverage() {
await prepareReportCoverageFolder(this.ctx)
}

async onFinished() {
const result: HTMLReportData = {
Copy link
Member Author

@userquin userquin Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic should be moved to onFinishedReportCoverage since the coverage still not generated:

  • running the standalone html reporter script will include ui client in the coverage report
  • we should remove the html folder on every run using standalone html reporter, otherwise the ui client from previous run will be included in the coverage report (when included the coverage inside the build)

Running this script from examples/basic folder:
pnpm vitest --ui --open --coverage.enabled=true --coverage.reporter=html --reporter=html --coverage.reportsDirectory=./html/coverage/

Running first time standalone html reporter (all good):

imagen

Running second time without cleanup html folder:

imagen

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

Successfully merging this pull request may close these issues.

4 participants