Skip to content

Commit 2c879cc

Browse files
committed
export hre reset function for plugin testing
1 parent 50c457d commit 2c879cc

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

v-next/hardhat-mocha-test-runner/test/fixture-projects/invalid-mocha-config/hardhat.config.js

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/config";
2+
3+
import HardhatMochaPlugin from "../../../src/index.js";
4+
5+
const config: HardhatUserConfig = {
6+
plugins: [HardhatMochaPlugin],
7+
mocha: {
8+
// @ts-expect-error -- testing config validation
9+
delay: 123,
10+
},
11+
};
12+
13+
export default config;

v-next/hardhat-mocha-test-runner/test/index.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import assert from "node:assert/strict";
2-
import { describe, it } from "node:test";
2+
import { describe, it, afterEach } from "node:test";
33

4-
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
4+
import { HardhatError } from "@ignored/hardhat-vnext-errors";
5+
import {
6+
assertRejectsWithHardhatError,
7+
useFixtureProject,
8+
} from "@nomicfoundation/hardhat-test-utils";
59

610
describe("Hardhat Mocha plugin", () => {
711
describe("Success", () => {
812
useFixtureProject("test-project");
913

14+
afterEach(async () => {
15+
const { _resetGlobalHardhatRuntimeEnvironment } = await import(
16+
"@ignored/hardhat-vnext"
17+
);
18+
19+
_resetGlobalHardhatRuntimeEnvironment();
20+
});
21+
1022
it("should work", async () => {
1123
const hre = await import("@ignored/hardhat-vnext");
1224

@@ -19,10 +31,23 @@ describe("Hardhat Mocha plugin", () => {
1931
describe("Failure", () => {
2032
useFixtureProject("invalid-mocha-config");
2133

34+
afterEach(async () => {
35+
const { _resetGlobalHardhatRuntimeEnvironment } = await import(
36+
"@ignored/hardhat-vnext"
37+
);
38+
39+
_resetGlobalHardhatRuntimeEnvironment();
40+
});
41+
2242
it("should fail", async () => {
23-
await assert.rejects(
24-
() => import("@ignored/hardhat-vnext"),
25-
/Config error in config\.mocha\.delay: Expected boolean, received number/,
43+
const errors =
44+
"\t* Config error in config.mocha.delay: Expected boolean, received number";
45+
46+
await assertRejectsWithHardhatError(
47+
// @ts-expect-error -- we need to invalidate the import cache to re-import the HRE
48+
import("@ignored/hardhat-vnext?config=invalid"),
49+
HardhatError.ERRORS.GENERAL.INVALID_CONFIG,
50+
{ errors },
2651
);
2752
});
2853
});

v-next/hardhat/src/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { createHardhatRuntimeEnvironment } from "./hre.js";
1212
import {
1313
getGlobalHardhatRuntimeEnvironment,
1414
setGlobalHardhatRuntimeEnvironment,
15+
resetGlobalHardhatRuntimeEnvironment,
1516
} from "./internal/global-hre-instance.js";
1617
import { importUserConfig } from "./internal/helpers/config-loading.js";
1718

@@ -38,4 +39,12 @@ export const globalOptions: GlobalOptions = hre.globalOptions;
3839
export const hooks: HookManager = hre.hooks;
3940
export const interruptions: UserInterruptionManager = hre.interruptions;
4041

42+
// We need to re-export this function so that plugins can use it.
43+
export const _resetGlobalHardhatRuntimeEnvironment: typeof resetGlobalHardhatRuntimeEnvironment =
44+
function (): void {
45+
maybeHre = undefined;
46+
47+
resetGlobalHardhatRuntimeEnvironment();
48+
};
49+
4150
export default hre;

0 commit comments

Comments
 (0)