-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,9 +38,9 @@ | |
"README.md" | ||
], | ||
"dependencies": { | ||
"@nomicfoundation/ethereumjs-util": "9.0.4", | ||
"@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", | ||
|
@@ -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:^", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Contrary to the rest of our packages, we do |
||
"@types/ci-info": "^2.0.0", | ||
"@types/debug": "^4.1.4", | ||
"@types/find-up": "^2.1.1", | ||
|
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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Node modules | ||
/node_modules | ||
|
||
# Compilation output | ||
/dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules | ||
/dist | ||
CHANGELOG.md |
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. |
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. |
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" | ||
} | ||
} |
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"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./hardhat-error.js"; |
There was a problem hiding this comment.
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