Skip to content

Commit 1fdcd02

Browse files
Improve error messages for viem assertions (#7384)
1 parent da546f2 commit 1fdcd02

File tree

26 files changed

+295
-94
lines changed

26 files changed

+295
-94
lines changed

.changeset/clever-days-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Fixed coverage report when loading data from large test suites ([#7385](https://github.com/NomicFoundation/hardhat/issues/7385))

.changeset/cold-shrimps-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-ethers": patch
3+
---
4+
5+
Fixed index resolution in clearEventListeners ([#7359](https://github.com/NomicFoundation/hardhat/pull/7359))

.changeset/healthy-dryers-mate.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
"@nomicfoundation/hardhat-errors": patch
33
"hardhat": patch
4-
"@nomicfoundation/hardhat-utils": patch
5-
"@nomicfoundation/hardhat-zod-utils": patch
64
---
75

86
Display an error message when attempting to use a global hardhat installation in a local repo ([#5362](https://github.com/NomicFoundation/hardhat/issues/5362))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@nomicfoundation/hardhat-mocha": patch
3+
"@nomicfoundation/hardhat-node-test-runner": patch
4+
"@nomicfoundation/hardhat-utils": patch
5+
"hardhat": patch
6+
---
7+
8+
Load resolved global options into environment variables during tests ([#7305](https://github.com/NomicFoundation/hardhat/pull/7305))

v-next/hardhat-ethers/src/internal/hardhat-ethers-provider/hardhat-ethers-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ export class HardhatEthersProvider implements HardhatEthersProviderI {
12831283
} else {
12841284
const index = await this.#findEventListenerIndex(event);
12851285

1286-
if (index === -1) {
1286+
if (index !== -1) {
12871287
const { listenersMap } = this.#eventListeners[index];
12881288
this.#eventListeners.splice(index, 1);
12891289
for (const blockListener of listenersMap.values()) {

v-next/hardhat-ethers/test/provider-events.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { HardhatEthers } from "../src/types.js";
2+
import type { EventFilter } from "ethers";
23
import type { EthereumProvider } from "hardhat/types/providers";
34

45
import assert from "node:assert/strict";
@@ -327,6 +328,30 @@ describe("provider events", () => {
327328
// remove subscription
328329
await ethers.provider.off("block", listener);
329330
});
331+
332+
it("should be a no-op when removeAllListeners(unregistered event filter) is called", async () => {
333+
const blockListener = mock.fn();
334+
await ethers.provider.on("block", blockListener);
335+
336+
// event filter that was never registered
337+
const unregisteredEventFilter: EventFilter = {
338+
address: "0x0000000000000000000000000000000000000001",
339+
topics: [],
340+
};
341+
342+
// should not affect existing block listeners
343+
await ethers.provider.removeAllListeners(unregisteredEventFilter);
344+
345+
// mine a block and ensure the block listener still fires
346+
const [s] = await ethers.getSigners();
347+
await s.sendTransaction({ to: s });
348+
349+
await tryUntil(() => {
350+
assert.equal(blockListener.mock.callCount(), 1);
351+
});
352+
353+
await ethers.provider.off("block", blockListener);
354+
});
330355
});
331356

332357
describe("listeners getters", () => {

v-next/hardhat-mocha/src/task-action.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { MochaOptions } from "mocha";
55
import { resolve as pathResolve } from "node:path";
66

77
import { HardhatError } from "@nomicfoundation/hardhat-errors";
8+
import { setGlobalOptionsAsEnvVariables } from "@nomicfoundation/hardhat-utils/env";
89
import { getAllFilesMatching } from "@nomicfoundation/hardhat-utils/fs";
910
import {
1011
markTestRunDone,
@@ -53,6 +54,8 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
5354
// Set an environment variable that plugins can use to detect when a process is running tests
5455
process.env.HH_TEST = "true";
5556

57+
setGlobalOptionsAsEnvVariables(hre.globalOptions);
58+
5659
if (!noCompile) {
5760
await hre.tasks.getTask("compile").run({});
5861
console.log();
@@ -71,10 +74,6 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
7174
imports.push(tsx.href);
7275

7376
if (hre.globalOptions.coverage === true) {
74-
// NOTE: We set the HARDHAT_COVERAGE environment variable here because, as of now,
75-
// the global options are not automatically passed to the child processes.
76-
process.env.HARDHAT_COVERAGE = "true";
77-
7877
const coverage = new URL(
7978
import.meta.resolve("@nomicfoundation/hardhat-mocha/coverage"),
8079
);
@@ -83,10 +82,6 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
8382
hre.config.test.mocha.require.push(coverage.href);
8483
}
8584

86-
if (hre.globalOptions.network !== undefined) {
87-
process.env.HARDHAT_NETWORK = hre.globalOptions.network;
88-
}
89-
9085
process.env.NODE_OPTIONS = imports
9186
.map((href) => `--import "${href}"`)
9287
.join(" ");

v-next/hardhat-node-test-runner/src/task-action.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { run } from "node:test";
77
import { URL } from "node:url";
88

99
import { hardhatTestReporter } from "@nomicfoundation/hardhat-node-test-reporter";
10+
import { setGlobalOptionsAsEnvVariables } from "@nomicfoundation/hardhat-utils/env";
1011
import { getAllFilesMatching } from "@nomicfoundation/hardhat-utils/fs";
1112
import { createNonClosingWriter } from "@nomicfoundation/hardhat-utils/stream";
1213
import { markTestRunStart, markTestRunDone } from "hardhat/internal/coverage";
@@ -59,6 +60,8 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
5960
// Set an environment variable that plugins can use to detect when a process is running tests
6061
process.env.HH_TEST = "true";
6162

63+
setGlobalOptionsAsEnvVariables(hre.globalOptions);
64+
6265
if (!noCompile) {
6366
await hre.tasks.getTask("compile").run({});
6467
console.log();
@@ -76,20 +79,12 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
7679
imports.push(tsx.href);
7780

7881
if (hre.globalOptions.coverage === true) {
79-
// NOTE: We set the HARDHAT_COVERAGE environment variable here because, as of now,
80-
// the global options are not automatically passed to the child processes.
81-
process.env.HARDHAT_COVERAGE = "true";
82-
8382
const coverage = new URL(
8483
import.meta.resolve("@nomicfoundation/hardhat-node-test-runner/coverage"),
8584
);
8685
imports.push(coverage.href);
8786
}
8887

89-
if (hre.globalOptions.network !== undefined) {
90-
process.env.HARDHAT_NETWORK = hre.globalOptions.network;
91-
}
92-
9388
process.env.NODE_OPTIONS = imports
9489
.map((href) => `--import "${href}"`)
9590
.join(" ");

v-next/hardhat-utils/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"./crypto": "./dist/src/crypto.js",
2121
"./date": "./dist/src/date.js",
2222
"./debug": "./dist/src/debug.js",
23+
"./env": "./dist/src/env.js",
2324
"./error": "./dist/src/error.js",
2425
"./eth": "./dist/src/eth.js",
2526
"./fs": "./dist/src/fs.js",
@@ -28,6 +29,7 @@
2829
"./lang": "./dist/src/lang.js",
2930
"./number": "./dist/src/number.js",
3031
"./package": "./dist/src/package.js",
32+
"./panic-errors": "./dist/src/panic-errors.js",
3133
"./path": "./dist/src/path.js",
3234
"./request": "./dist/src/request.js",
3335
"./string": "./dist/src/string.js",

v-next/hardhat-utils/src/env.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { camelToSnakeCase } from "./string.js";
2+
3+
/**
4+
* Sets the resolved global options as environment variables.
5+
*
6+
* @param globalOptions An object containing the resolved global options,
7+
* with each option adhering to its definition in the globalOptionDefinitions.
8+
*/
9+
export function setGlobalOptionsAsEnvVariables<
10+
T extends Record<keyof T, string | boolean>,
11+
>(globalOptions: T): void {
12+
for (const [name, value] of Object.entries(globalOptions)) {
13+
const envName = getEnvVariableNameFromGlobalOption(name);
14+
15+
if (value !== undefined) {
16+
process.env[envName] = String(value);
17+
}
18+
}
19+
}
20+
21+
/**
22+
* Converts a global option name to its corresponding environment variable name.
23+
* The conversion involves transforming the option name from camelCase to
24+
* SNAKE_CASE and prefixing it with "HARDHAT_".
25+
*
26+
* @param globalOptionName The name of the global option in camelCase.
27+
*
28+
* @returns The corresponding environment variable name in the format
29+
* "HARDHAT_<OPTION_NAME_IN_SNAKE_CASE>".
30+
*/
31+
export function getEnvVariableNameFromGlobalOption(globalOptionName: string) {
32+
return `HARDHAT_${camelToSnakeCase(globalOptionName).toUpperCase()}`;
33+
}

0 commit comments

Comments
 (0)