Skip to content

Commit 29cef28

Browse files
authored
Merge pull request #5492 from NomicFoundation/hardhat-test-utils
Add `hardhat-test-utils` package
2 parents 47c4228 + bdf581a commit 29cef28

19 files changed

+421
-1
lines changed

pnpm-lock.yaml

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v-next/hardhat-build-system/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
"README.md"
3939
],
4040
"dependencies": {
41-
"@nomicfoundation/ethereumjs-util": "9.0.4",
4241
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.0",
4342
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.0",
43+
"@nomicfoundation/ethereumjs-util": "9.0.4",
4444
"@nomicfoundation/solidity-analyzer": "^0.1.0",
4545
"adm-zip": "^0.4.16",
4646
"aggregate-error": "^3.0.0",
@@ -61,6 +61,7 @@
6161
"devDependencies": {
6262
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
6363
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.0",
64+
"@nomicfoundation/hardhat-test-utils": "workspace:^",
6465
"@types/ci-info": "^2.0.0",
6566
"@types/debug": "^4.1.4",
6667
"@types/find-up": "^2.1.1",

v-next/hardhat-build-system/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{
88
"path": "../hardhat-node-test-reporter"
99
},
10+
{
11+
"path": "../hardhat-test-utils"
12+
},
1013
{
1114
"path": "../hardhat-utils"
1215
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { createConfig } = require("../../config-v-next/eslint.cjs");
2+
3+
module.exports = createConfig(__filename);

v-next/hardhat-test-utils/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Node modules
2+
/node_modules
3+
4+
# Compilation output
5+
/dist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/dist
3+
CHANGELOG.md

v-next/hardhat-test-utils/LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Nomic Foundation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

v-next/hardhat-test-utils/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Hardhat test utils
2+
3+
This is an internal package that includes test utilities used during the development of Hardhat.
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "@nomicfoundation/hardhat-test-utils",
3+
"private": true,
4+
"version": "3.0.0",
5+
"description": "Test utilities used to develop and test Hardhat",
6+
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-test-utils",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/NomicFoundation/hardhat",
10+
"directory": "v-next/hardhat-test-utils"
11+
},
12+
"author": "Nomic Foundation",
13+
"license": "MIT",
14+
"type": "module",
15+
"exports": {
16+
".": "./dist/src/index.js"
17+
},
18+
"scripts": {
19+
"lint": "pnpm prettier --check && pnpm eslint",
20+
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
21+
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
22+
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
23+
"test": "node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
24+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
25+
"pretest": "pnpm build",
26+
"build": "tsc --build .",
27+
"prepublishOnly": "pnpm build",
28+
"clean": "rimraf dist"
29+
},
30+
"files": [
31+
"dist/src/",
32+
"src/",
33+
"CHANGELOG.md",
34+
"LICENSE",
35+
"README.md"
36+
],
37+
"devDependencies": {
38+
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
39+
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
40+
"@types/node": "^20.14.9",
41+
"@typescript-eslint/eslint-plugin": "^7.7.1",
42+
"@typescript-eslint/parser": "^7.7.1",
43+
"eslint": "8.57.0",
44+
"eslint-config-prettier": "9.1.0",
45+
"eslint-import-resolver-typescript": "^3.6.1",
46+
"eslint-plugin-import": "2.29.1",
47+
"eslint-plugin-no-only-tests": "3.1.0",
48+
"expect-type": "^0.19.0",
49+
"prettier": "3.2.5",
50+
"rimraf": "^5.0.5",
51+
"tsx": "^4.11.0",
52+
"typescript": "~5.5.0",
53+
"typescript-eslint": "7.7.1"
54+
},
55+
"dependencies": {
56+
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
57+
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2"
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { ErrorDescriptor } from "@ignored/hardhat-vnext-errors";
2+
3+
import assert from "node:assert/strict";
4+
5+
import { HardhatError } from "@ignored/hardhat-vnext-errors";
6+
import { ensureError } from "@ignored/hardhat-vnext-utils/error";
7+
8+
export function assertIsHardhatError<ErrorDescriptorT extends ErrorDescriptor>(
9+
error: unknown,
10+
descritor: ErrorDescriptorT,
11+
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
12+
): asserts error is HardhatError<ErrorDescriptorT> {
13+
assert.ok(HardhatError.isHardhatError(error), "Error is not a HardhatError");
14+
15+
// We first compare the number individually to throw a better error message
16+
// in case the descriptors are different.
17+
assert.equal(
18+
error.descriptor.number,
19+
descritor.number,
20+
`Expected error number ${descritor.number}, but got ${error.descriptor.number}`,
21+
);
22+
23+
assert.deepEqual(error.descriptor, descritor);
24+
25+
assert.deepEqual(error.messageArguments, messageArguments);
26+
}
27+
28+
export function assertThrowsHardhatError<
29+
ErrorDescriptorT extends ErrorDescriptor,
30+
>(
31+
f: () => any,
32+
descritor: ErrorDescriptorT,
33+
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
34+
): void {
35+
try {
36+
f();
37+
} catch (error) {
38+
ensureError(error);
39+
assertIsHardhatError(error, descritor, messageArguments);
40+
41+
return;
42+
}
43+
44+
assert.fail("Function did not throw any error");
45+
}
46+
47+
export async function assertRejectsWithHardhatError<
48+
ErrorDescriptorT extends ErrorDescriptor,
49+
>(
50+
f: (() => Promise<any>) | Promise<any>,
51+
descritor: ErrorDescriptorT,
52+
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
53+
): Promise<void> {
54+
try {
55+
if (f instanceof Promise) {
56+
await f;
57+
} else {
58+
await f();
59+
}
60+
} catch (error) {
61+
ensureError(error);
62+
assertIsHardhatError(error, descritor, messageArguments);
63+
64+
return;
65+
}
66+
67+
assert.fail("Function did not throw any error");
68+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./hardhat-error.js";

0 commit comments

Comments
 (0)