Skip to content

Commit 4452d28

Browse files
committed
change errors to HardhatErrors and reset hre singleton in tests
1 parent dafef6b commit 4452d28

File tree

7 files changed

+67
-35
lines changed

7 files changed

+67
-35
lines changed

v-next/hardhat-errors/src/descriptors.ts

+26
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,32 @@ Please double check the file path.`,
113113
114114
Please double check whether you have multiple versions of the same plugin installed.`,
115115
},
116+
NO_CONFIG_FILE_FOUND: {
117+
number: 5,
118+
messageTemplate: "No Hardhat config file found",
119+
websiteTitle: "No Hardhat config file found",
120+
websiteDescription:
121+
"Hardhat couldn't find a config file in the current directory or any of its parents.",
122+
},
123+
INVALID_CONFIG_PATH: {
124+
number: 6,
125+
messageTemplate: "Config file %configPath% not found",
126+
websiteTitle: "Invalid config path",
127+
websiteDescription: "The config file doesn't exist at the provided path.",
128+
},
129+
NO_CONFIG_EXPORTED: {
130+
number: 7,
131+
messageTemplate: "No config exported in %configPath%",
132+
websiteTitle: "No config exported",
133+
websiteDescription: "There is nothing exported from the config file.",
134+
},
135+
INVALID_CONFIG_OBJECT: {
136+
number: 8,
137+
messageTemplate: "Invalid config exported in %configPath%",
138+
websiteTitle: "Invalid config object",
139+
websiteDescription:
140+
"The config file doesn't export a valid configuration object.",
141+
},
116142
},
117143
INTERNAL: {
118144
ASSERTION_ERROR: {

v-next/hardhat/src/internal/helpers/config-loading.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { isAbsolute, resolve } from "node:path";
2+
import { pathToFileURL } from "node:url";
23

4+
import { HardhatError } from "@nomicfoundation/hardhat-errors";
35
import { findUp } from "@nomicfoundation/hardhat-utils/fs";
46

7+
import { ERRORS } from "../../../../hardhat-errors/src/descriptors.js";
8+
59
async function findClosestHardhatConfig(): Promise<string> {
6-
let hardhatConfigPath = await findUp("hardhat.config.js");
10+
let hardhatConfigPath = await findUp("hardhat.config.ts");
711

812
if (hardhatConfigPath !== undefined) {
913
return hardhatConfigPath;
1014
}
1115

12-
hardhatConfigPath = await findUp("hardhat.config.ts");
16+
hardhatConfigPath = await findUp("hardhat.config.js");
1317

1418
if (hardhatConfigPath !== undefined) {
1519
return hardhatConfigPath;
1620
}
1721

18-
throw new Error("No Hardhat config file found");
22+
throw new HardhatError(ERRORS.GENERAL.NO_CONFIG_FILE_FOUND);
1923
}
2024

2125
export async function resolveConfigPath(): Promise<string> {
@@ -36,19 +40,22 @@ export async function importUserConfig(configPath: string) {
3640
const { exists } = await import("@nomicfoundation/hardhat-utils/fs");
3741

3842
if (!(await exists(normalizedPath))) {
39-
throw new Error(`Config file ${configPath} not found`);
43+
throw new HardhatError(ERRORS.GENERAL.INVALID_CONFIG_PATH, { configPath });
4044
}
4145

42-
const imported = await import(normalizedPath);
46+
console.log("also here", pathToFileURL(normalizedPath).href);
47+
const imported = await import(pathToFileURL(normalizedPath).href);
4348

4449
if (!("default" in imported)) {
45-
throw new Error(`No config exported in ${configPath}`);
50+
throw new HardhatError(ERRORS.GENERAL.NO_CONFIG_EXPORTED, { configPath });
4651
}
4752

4853
const config = imported.default;
4954

5055
if (typeof config !== "object" || config === null) {
51-
throw new Error(`Invalid config exported in ${configPath}`);
56+
throw new HardhatError(ERRORS.GENERAL.INVALID_CONFIG_OBJECT, {
57+
configPath,
58+
});
5259
}
5360

5461
return config;

v-next/hardhat/src/internal/hre-singleton.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { HardhatRuntimeEnvironment } from "../types/hre.js";
22

33
import { createHardhatRuntimeEnvironment } from "@nomicfoundation/hardhat-core";
44

5-
let hre: HardhatRuntimeEnvironment;
5+
let hre: HardhatRuntimeEnvironment | undefined;
66

77
/**
88
* This function returns a singleton instance of the Hardhat Runtime Environment.
@@ -20,3 +20,12 @@ export async function getHardhatRuntimeEnvironmentSingleton(
2020

2121
return hre;
2222
}
23+
24+
/**
25+
* This function resets the singleton instance of the Hardhat Runtime Environment.
26+
*
27+
* It should be used only in tests.
28+
*/
29+
export function resetHardhatRuntimeEnvironmentSingleton() {
30+
hre = undefined;
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// left empty so github commits the folder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// left empty so github commits the folder

v-next/hardhat/test/helpers/project.ts

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import fsPromises from "node:fs/promises";
21
import path from "node:path";
32
import { before, after } from "node:test";
43

5-
import { exists } from "@nomicfoundation/hardhat-utils/fs";
4+
import { exists, getRealPath } from "@nomicfoundation/hardhat-utils/fs";
65

76
/**
87
* This helper adds mocha hooks to run the tests inside one of the projects
@@ -50,18 +49,3 @@ async function getFixtureProjectPath(
5049

5150
return getRealPath(projectPath);
5251
}
53-
54-
/**
55-
* Returns the real path of absolutePath, resolving symlinks.
56-
*
57-
* @throws Error if absolutePath doesn't exist.
58-
*/
59-
async function getRealPath(absolutePath: string): Promise<string> {
60-
try {
61-
// This method returns the actual casing.
62-
// Please read Node.js' docs to learn more.
63-
return await fsPromises.realpath(path.normalize(absolutePath));
64-
} catch {
65-
throw new Error(`Invalid directory ${absolutePath}`);
66-
}
67-
}

v-next/hardhat/test/hre/index.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { describe, it } from "node:test";
44
import { createHardhatRuntimeEnvironment } from "../../src/hre.js";
55
import { builtinPlugins } from "../../src/internal/builtin-plugins/index.js";
66
import { resolveConfigPath } from "../../src/internal/helpers/config-loading.js";
7-
import { getHardhatRuntimeEnvironmentSingleton } from "../../src/internal/hre-singleton.js";
7+
import {
8+
getHardhatRuntimeEnvironmentSingleton,
9+
resetHardhatRuntimeEnvironmentSingleton,
10+
} from "../../src/internal/hre-singleton.js";
811
import { useFixtureProject } from "../helpers/project.js";
912

1013
describe("HRE", () => {
@@ -13,6 +16,8 @@ describe("HRE", () => {
1316
const hre = await createHardhatRuntimeEnvironment({});
1417

1518
assert.deepEqual(hre.config.plugins, builtinPlugins);
19+
20+
resetHardhatRuntimeEnvironmentSingleton();
1621
});
1722
});
1823

@@ -32,22 +37,24 @@ describe("HRE", () => {
3237
{ id: "custom task" },
3338
);
3439
assert.deepEqual(hre1, hre2);
40+
41+
resetHardhatRuntimeEnvironmentSingleton();
3542
});
3643
});
3744

3845
describe("config loading", () => {
3946
describe("resolveConfigPath", async () => {
4047
it("should return the HARDHAT_CONFIG env variable if it is set", async () => {
41-
process.env.HARDHAT_CONFIG = "hardhat.config.js";
48+
process.env.HARDHAT_CONFIG = "env.config.js";
4249

43-
assert.equal(await resolveConfigPath(), "hardhat.config.js");
50+
assert.equal(await resolveConfigPath(), "env.config.js");
4451

4552
delete process.env.HARDHAT_CONFIG;
4653
});
4754

4855
it("should throw if the config file is not found", async () => {
4956
await assert.rejects(resolveConfigPath(), {
50-
message: "No Hardhat config file found",
57+
message: "HHE5: No Hardhat config file found",
5158
});
5259
});
5360

@@ -96,18 +103,15 @@ describe("HRE", () => {
96103
});
97104
});
98105

99-
// This test works individually but fails when running all tests
100-
// due to the hre singleton being used in tests above.
101-
// ESM modules cache is not accessible like `require.cache` in CJS,
102-
// so a workaround is needed.
103-
// TODO: Fix this test
104-
describe.skip("programmatic API", () => {
106+
describe("programmatic API", () => {
105107
useFixtureProject("loaded-config");
106108

107109
it("should load the config file", async () => {
108110
const hre = await import("../../src/index.js");
109111

110112
assert.deepEqual(hre.config.plugins, [{ id: "test-plugin" }]);
113+
114+
resetHardhatRuntimeEnvironmentSingleton();
111115
});
112116
});
113117
});

0 commit comments

Comments
 (0)