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

Feature: add chai matchers for viem #5252

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/stupid-dogs-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/hardhat-chai-matchers-viem": major
---

Initial release, forked from @nomicfoundation/[email protected]
92 changes: 92 additions & 0 deletions .github/workflows/hardhat-chai-matchers-viem-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: hardhat-chai-matchers-viem CI

on:
push:
branches: [$default-branch]
paths:
- "packages/hardhat-chai-matchers-viem/**"
- "packages/hardhat-common/**"
- "config/**"
pull_request:
branches:
- "**"
paths:
- "packages/hardhat-chai-matchers-viem/**"
- "packages/hardhat-common/**"
- "config/**"
workflow_dispatch:

defaults:
run:
working-directory: packages/hardhat-chai-matchers-viem

concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: true

jobs:
test_on_windows:
name: Test hardhat-chai-matchers-viem on Windows with Node 18
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: actions/setup-node@v4
with:
node-version: 18
cache: "pnpm"
- name: Install
run: pnpm install --frozen-lockfile --prefer-offline
- name: Build
run: pnpm build
- name: Run tests
env:
FORCE_COLOR: 3
run: pnpm test:ci

test_on_macos:
name: Test hardhat-chai-matchers-viem on MacOS with Node 18
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: actions/setup-node@v4
with:
node-version: 18
cache: "pnpm"
- name: Install
run: pnpm install --frozen-lockfile --prefer-offline
- name: Build
run: pnpm build
- name: Run tests
env:
FORCE_COLOR: 3
run: pnpm test:ci

test_on_linux:
name: Test hardhat-chai-matchers-viem on Ubuntu with Node ${{ matrix.node }}
runs-on: ubuntu-latest
strategy:
matrix:
node: [18, 20]
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: "pnpm"
- name: Install
run: pnpm install --frozen-lockfile --prefer-offline
- name: Build
run: pnpm build
- name: Run tests
env:
FORCE_COLOR: 3
run: pnpm test:ci
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
},
"scripts": {
"postbuild": "cp packages/hardhat-core/src/internal/solidity/compiler/solcjs-runner.js packages/hardhat-core/internal/solidity/compiler/solcjs-runner.js",
"build": "tsc --build packages/hardhat-core packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-v4 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
"watch": "tsc --build --watch packages/hardhat-core/src packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-v4 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
"build": "tsc --build packages/hardhat-core packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-v4 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-chai-matchers-viem packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
"watch": "tsc --build --watch packages/hardhat-core/src packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-v4 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-chai-matchers-viem packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
"clean": "pnpm run --recursive clean",
"test": "node scripts/run-tests.js",
"lint": "pnpm run --recursive lint && pnpm prettier --check",
Expand Down
32 changes: 32 additions & 0 deletions packages/hardhat-chai-matchers-viem/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const {
slowImportsCommonIgnoredModules,
} = require("../../config/eslint/constants");

module.exports = {
extends: [`${__dirname}/../../config/eslint/eslintrc.js`],
parserOptions: {
project: `${__dirname}/src/tsconfig.json`,
sourceType: "module",
},
rules: {
"@typescript-eslint/no-non-null-assertion": "error",
},
overrides: [
{
files: ["src/index.ts"],
rules: {
"@nomicfoundation/slow-imports/no-top-level-external-import": [
"error",
{
ignoreModules: [
...slowImportsCommonIgnoredModules,
"chai",
"chai-as-promised",
"@nomicfoundation/hardhat-viem",
],
},
],
},
},
],
};
92 changes: 92 additions & 0 deletions packages/hardhat-chai-matchers-viem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Node modules
/node_modules

# Compilation output
/build-test/
/dist
/internal
/types
/*.js
/*.js.map
/*.d.ts
/*.d.ts.map

# Code coverage artifacts
/coverage
/.nyc_output

# Below is Github's node gitignore template,
# ignoring the node_modules part, as it'd ignore every node_modules, and we have some for testing

# Logs
logs
*.log

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
#node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'pnpm pack'
*.tgz

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

!.eslintrc.js
5 changes: 5 additions & 0 deletions packages/hardhat-chai-matchers-viem/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": "ts-node/register/files",
"ignore": ["test/fixture-projects/**/*"],
"timeout": 10000
}
13 changes: 13 additions & 0 deletions packages/hardhat-chai-matchers-viem/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/node_modules
/dist
/internal
/types
/*.d.ts
/*.d.ts.map
/*.js
/*.js.map
/build-test
/test/fixture-projects/**/artifacts
/test/fixture-projects/**/cache
CHANGELOG.md
!.eslintrc.js
7 changes: 7 additions & 0 deletions packages/hardhat-chai-matchers-viem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @nomicfoundation/hardhat-chai-matchers-viem

## 2.0.7

### Major Changes

- : Initial release, forked from @nomicfoundation/[email protected]
21 changes: 21 additions & 0 deletions packages/hardhat-chai-matchers-viem/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 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.
84 changes: 84 additions & 0 deletions packages/hardhat-chai-matchers-viem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[![npm](https://img.shields.io/npm/v/@nomicfoundation/hardhat-chai-matchers-viem.svg)](https://www.npmjs.com/package/@nomicfoundation/hardhat-chai-matchers-viem)

# Hardhat Chai Matchers Viem

This plugin adds Ethereum-specific capabilities to the [Chai](https://chaijs.com/) assertion library, making your smart contract tests easy to write and read.

It is based on @nomicfoundation/hardhat-chai-matchers which is for ethers. Check [its documentation](https://hardhat.org/hardhat-chai-matchers/docs) to learn more.

### Installation

We recommend using npm 7 or later. If you do that, then you just need to install the plugin itself:

```bash
npm install --save-dev @nomicfoundation/hardhat-chai-matchers-viem
```

If you are using an older version of npm, you'll also need to install all the packages used by the plugin.

```bash
npm install --save-dev @nomicfoundation/hardhat-chai-matchers-viem chai@4 @nomicfoundation/hardhat-viem viem
```

That's also the case if you are using yarn:

```bash
yarn add --dev @nomicfoundation/hardhat-chai-matchers-viem chai@4 @nomicfoundation/hardhat-viem viem
```

### Usage

After installing it, add the plugin to your Hardhat config:

```js
require("@nomicfoundation/hardhat-chai-matchers-viem");
```

Then you'll be able to use the matchers in your tests:

```js
expect(await token.read.totalSupply()).to.equal(1_000_000);

await expect(token.write.transfer([token, 1000n])).to.be.revertedWith(
"Cannot transfer to the contract itself"
);

await expect(token.write.transfer([recipient, 1000n]))
.to.emit(token, "Transfer")
.withArgs(owner, recipient, 1000);
```

### Known issues

#### Chaining Async Matchers

Currently, the following matchers do not support chaining:

- `reverted`
- `revertedWith`
- `revertedWithCustomError`
- `revertedWithoutReason`
- `revertedWithPanic`
- `changeEtherBalance`
- `changeEtherBalances`
- `changeTokenBalance`
- `changeTokenBalances`
- `emit` (with the only exception of chaining multiple `emit` matchers)

Which means you can't do:

```js
await expect(contract.f(...))
.to.changeEtherBalance(...)
.and.to.changeTokenBalance(...)
```

To work around this limitation, write separate assertions for each matcher:

```js
const tx = contract.f(...);
await expect(tx).to.changeEtherBalance(...)
await expect(tx).to.changeTokenBalance(...)
```

If you are interested in seeing an implementation of chaining for async matchers, please visit the GitHub issue [#4235](https://github.com/NomicFoundation/hardhat/issues/4235) and leave an upvote or comment.
Loading