Skip to content
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

Add hardhat-test-utils package #5492

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion v-next/hardhat-build-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"README.md"
],
"dependencies": {
"@nomicfoundation/ethereumjs-util": "9.0.4",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jus an automatic reordering that pnpm did

"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.0",
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.0",
"@nomicfoundation/ethereumjs-util": "9.0.4",
"@nomicfoundation/solidity-analyzer": "^0.1.0",
"adm-zip": "^0.4.16",
"aggregate-error": "^3.0.0",
Expand All @@ -61,6 +61,7 @@
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.0",
"@nomicfoundation/hardhat-test-utils": "workspace:^",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contrary to the rest of our packages, we do workspace:^ here because this is a private package, which we don't intend to publish, so versioning is not an issue.

"@types/ci-info": "^2.0.0",
"@types/debug": "^4.1.4",
"@types/find-up": "^2.1.1",
Expand Down
3 changes: 3 additions & 0 deletions v-next/hardhat-build-system/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
{
"path": "../hardhat-node-test-reporter"
},
{
"path": "../hardhat-test-utils"
},
{
"path": "../hardhat-utils"
}
Expand Down
3 changes: 3 additions & 0 deletions v-next/hardhat-test-utils/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { createConfig } = require("../../config-v-next/eslint.cjs");

module.exports = createConfig(__filename);
5 changes: 5 additions & 0 deletions v-next/hardhat-test-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Node modules
/node_modules

# Compilation output
/dist
3 changes: 3 additions & 0 deletions v-next/hardhat-test-utils/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/dist
CHANGELOG.md
9 changes: 9 additions & 0 deletions v-next/hardhat-test-utils/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2024 Nomic Foundation

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
3 changes: 3 additions & 0 deletions v-next/hardhat-test-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hardhat test utils

This is an internal package that includes test utilities used during the development of Hardhat.
59 changes: 59 additions & 0 deletions v-next/hardhat-test-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "@nomicfoundation/hardhat-test-utils",
"private": true,
"version": "3.0.0",
"description": "Test utilities used to develop and test Hardhat",
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-test-utils",
"repository": {
"type": "git",
"url": "https://github.com/NomicFoundation/hardhat",
"directory": "v-next/hardhat-test-utils"
},
"author": "Nomic Foundation",
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/src/index.js"
},
"scripts": {
"lint": "pnpm prettier --check && pnpm eslint",
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
"test": "node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
"pretest": "pnpm build",
"build": "tsc --build .",
"prepublishOnly": "pnpm build",
"clean": "rimraf dist"
},
"files": [
"dist/src/",
"src/",
"CHANGELOG.md",
"LICENSE",
"README.md"
],
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-no-only-tests": "3.1.0",
"expect-type": "^0.19.0",
"prettier": "3.2.5",
"rimraf": "^5.0.5",
"tsx": "^4.11.0",
"typescript": "~5.5.0",
"typescript-eslint": "7.7.1"
},
"dependencies": {
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2"
}
}
68 changes: 68 additions & 0 deletions v-next/hardhat-test-utils/src/hardhat-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { ErrorDescriptor } from "@ignored/hardhat-vnext-errors";

import assert from "node:assert/strict";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { ensureError } from "@ignored/hardhat-vnext-utils/error";

export function assertIsHardhatError<ErrorDescriptorT extends ErrorDescriptor>(
error: unknown,
descritor: ErrorDescriptorT,
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
): asserts error is HardhatError<ErrorDescriptorT> {
assert.ok(HardhatError.isHardhatError(error), "Error is not a HardhatError");

// We first compare the number individually to throw a better error message
// in case the descriptors are different.
assert.equal(
error.descriptor.number,
descritor.number,
`Expected error number ${descritor.number}, but got ${error.descriptor.number}`,
);

assert.deepEqual(error.descriptor, descritor);

assert.deepEqual(error.messageArguments, messageArguments);
}

export function assertThrowsHardhatError<
ErrorDescriptorT extends ErrorDescriptor,
>(
f: () => any,
descritor: ErrorDescriptorT,
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
): void {
try {
f();
} catch (error) {
ensureError(error);
assertIsHardhatError(error, descritor, messageArguments);

return;
}

assert.fail("Function did not throw any error");
}

export async function assertRejectsWithHardhatError<
ErrorDescriptorT extends ErrorDescriptor,
>(
f: (() => Promise<any>) | Promise<any>,
descritor: ErrorDescriptorT,
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
): Promise<void> {
try {
if (f instanceof Promise) {
await f;
} else {
await f();
}
} catch (error) {
ensureError(error);
assertIsHardhatError(error, descritor, messageArguments);

return;
}

assert.fail("Function did not throw any error");
}
1 change: 1 addition & 0 deletions v-next/hardhat-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./hardhat-error.js";
Loading