-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[html coverage report] Add vendored package + generate HTML report #7528
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: b26e193 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
hardhatTotal size of the bundle: List of dependencies (sorted by size) |
v-next/hardhat/assets/index.js
Outdated
| @@ -0,0 +1,2 @@ | |||
| import libCoverage from "./lib-coverage/index.cjs"; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a "bridge" to expose the common js code into ESM js code, so that the ts file can then import it
| "pretest": "pnpm build", | ||
| "pretest:only": "pnpm build", | ||
| "build": "tsc --build .", | ||
| "postbuild": "node dist/src/copy-assets.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This step is required to copy the assets folder, which contains the CSS and JS files used to generate the report, into the dist folder after compilation
| @@ -0,0 +1,33 @@ | |||
| import { cp } from "node:fs/promises"; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is invoked by the postbuild in the package.json file.
Used to copy the assets folder, which contains the CSS and JS files used to generate the report, into the dist folder after compilation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to better ideas to solve this issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this PR with verdaccio and there were no issues, so this seems to work fine and it's simple enough.
| @@ -0,0 +1,13 @@ | |||
| { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need a double-check here. I copied the configuration from tsconfig.base.json, but removed all the properties that would cause errors during compilation, since this folder contains non-ESM files
| report: Report, | ||
| htmlReportPath: string, | ||
| ): Promise<void> { | ||
| const baseDir = process.cwd(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is intentionally empty to prevent build errors and avoid changing the default scripts in package.json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a comment inside the file.
| "@nomicfoundation/hardhat": minor | ||
| "@nomicfoundation/hardhat-vendored": patch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of things:
@nomicfoundation/hardhatshould behardhat- The changeset for Hardhat should be patch. I don't think this merits a minor bump.
@nomicfoundation/hardhat-vendoredis a new package, so it shouldn't be included here. Otherwise its first version will be 3.0.1, not 3.0.0. I checked locally thatchangeset publishwill publishhardhat-vendoredeven if it doesn't have a changeset, because it detects that it's not published yet. (I'm not entirely sure that the CI will have the same behavior, but I don't see why it wouldn't.)
| "@nomicfoundation/hardhat": minor | |
| "@nomicfoundation/hardhat-vendored": patch | |
| "hardhat": patch |
| await writeUtf8File(lcovReportPath, lcovReport); | ||
| log(`Saved lcov report to ${lcovReportPath}`); | ||
|
|
||
| const htmlReportPath = path.join(this.#coveragePath, "html"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nyc, jest and vitest all dump the html inside the coverage directory, even when both the HTML and lcov reporters are used together. I personally assume that coverage/index.html will exist and was a bit surprised when it didn't.
I'm not against using a subdirectory for this (in fact, it would be my preference if we were doing this from scratch), but I think it's better to be consistent with other tools here.
| const htmlReportPath = path.join(this.#coveragePath, "html"); | |
| const htmlReportPath = path.join(this.#coveragePath); |
| "@nomicfoundation/hardhat-vendored": patch | ||
| --- | ||
|
|
||
| Added HTML coverage reporting and a new internal `hardhat-vendored` package ([7528](https://github.com/NomicFoundation/hardhat/pull/7528)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changesets shouldn't include implementation details.
| Added HTML coverage reporting and a new internal `hardhat-vendored` package ([7528](https://github.com/NomicFoundation/hardhat/pull/7528)). | |
| Added HTML coverage reporting ([7528](https://github.com/NomicFoundation/hardhat/pull/7528)). |
|
|
||
| const htmlReportPath = path.join(this.#coveragePath, "html"); | ||
| await this.#writeHtmlReport(report, htmlReportPath); | ||
| console.log(`Saved html report to ${htmlReportPath}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant to use a debug log here.
| console.log(`Saved html report to ${htmlReportPath}`); | |
| log(`Saved html report to ${htmlReportPath}`); |
| // Construct coverage data for each tested file, | ||
| // detailing whether each line was executed or not. | ||
| for (const [p, fileCoverageInput] of Object.entries(report)) { | ||
| const testedFilePath = path.isAbsolute(p) ? p : path.join(baseDir, p); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker, but: I am surprised about this. We generate the report, so we should know if the keys are relative or absolute paths. (The Report interface names the key relativePath, so I guess they are meant to be relative).
Another concern is that, if they are relative, it's unclear whether they are relative to the cwd or the project's root (IMO it should be the latter).
Again: I wouldn't do anything in this PR, but this seems like technical debt. Not sure how/if we are capturing those.
| coverageMap, | ||
| }); | ||
|
|
||
| reports.create("html", undefined).execute(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional parameter with default value.
| reports.create("html", undefined).execute(context); | |
| reports.create("html").execute(context); |
| continue; | ||
| } | ||
|
|
||
| // TODO: bug line count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this mean?
| "name": "@nomicfoundation/hardhat-vendored", | ||
| "version": "3.0.0", | ||
| "description": "Internal dependencies used by Hardhat that have been vendored to prevent bloating the main package", | ||
| "homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-vendored", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not using the v-next branch anymore.
| "homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-vendored", | |
| "homepage": "https://github.com/nomicfoundation/hardhat/tree/main/v-next/hardhat-vendored", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a comment inside the file.
| @@ -0,0 +1,33 @@ | |||
| import { cp } from "node:fs/promises"; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this PR with verdaccio and there were no issues, so this seems to work fine and it's simple enough.


Link to internal design doc: https://www.notion.so/nomicfoundation/Code-coverage-HTML-report-286578cdeaf580bcad22db58ce54fb47?source=copy_link
As listed in the design doc, this PR addresses point number 1 out of 3:
TODOs before merge
NOTEs for review
v-next/hardhat-vendored/src/coverage-modulehas been copied from vendored packages, so there’s no need for a deep review, a quick scan will be sufficient