-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: main
Are you sure you want to change the base?
Feature: add chai matchers for viem #5252
Conversation
🦋 Changeset detectedLatest commit: bb45ca8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Hey, thanks for submitting this pr! I'll review it asap and we can discuss how to move forward |
Sounds good. After using this for a few days, I think it might be worth updating the addressable stuff to lowercase (or convert to checksum) any address it find in a .equal or .not.equal matcher before comparison. It's annoying to have to use viem's getAddress() constantly (or .toLowerCase()) because RPC calls typically return lowercase addresses (contract addresses, wallet address, etc), while viem contract calls return checksummed addresses (contract.read.someAddress()). |
hey, i also wrote something very similar here just last week! |
Hah, wish I knew that before I started this! At first glance, definitely some useful things in your more opinionated version. Could always bring the good stuff from your version into the ethers one. |
👋 for a new project I switched from ethers to viem and I was expecting a better developer experience. So far it's good, but today I discovered that I cannot use the chai matchers I'm used to, specifically the ones to check if a tx reverted or if it emitted events. Once it's ready, it would be great to see this PR merged, as it would help devs to better test their contracts (plus I don't want to go back to ethers only for this). |
You can use this for now: https://www.npmjs.com/package/hardhat-chai-matchers-viem |
@alcuadrado any updates on this? |
Hey @johnpm-12, we are currently in depths of our Hardhat v3 work, a major overhaul. |
This adds a new package hardhat-chai-matchers-viem which is forked from hardhat-chai-matchers but supports viem instead of ethers.
I originally planned to update the existing ethers-based package to support both viem and ethers, but that proved to be difficult. So I made it a separate package, similar to how there is hardhat-toolbox and hardhat-toolbox-viem.
I understand that issue #4874 was to make the existing package compatible, so if that's still the plan, no skin off my back if ya'll decide not to merge this. I wrote this because I needed it internally. It was very time consuming. I pushed it to npm if anyone else finds it useful in the meantime: https://www.npmjs.com/package/hardhat-chai-matchers-viem
All existing tests have been updated to use viem instead of ethers, and all tests pass. However, viem with an external hardhat node does not return contract error data in some cases, so the tests to use an external hardhat node are skipped. All tests pass with the in-process hardhat node. I was going to parse the viem error to piece together what I can about the contract error, but I figured it would be better if the hardhat node or viem or hardhat-viem were updated to include the contract error data instead.
Linting succeeds.
I don't know what your process is for publishing to npm, so I'm not sure if anything needs to be done to add a new package there.
If you want to compare this package to the ethers one, you can use
git diff --no-index packages/hardhat-chai-matchers/src/index.ts packages/hardhat-chai-matchers-viem/src/index.ts
in the root folder, for example. VS Code also has a command to compare 2 local files.