NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.29k stars 1.41k forks source link

Feature Request: Implement Chaining for Async Matchers #4235

Open schaable opened 1 year ago

schaable commented 1 year ago

Describe the feature

The hardhat-chai-matchers plugin currently lacks support for chaining with async matchers such as reverted, revertedWith, changeEtherBalance, etc.

Chaining would enable users to create more expressive and readable assertions by combining multiple matchers, improving the testing experience.

Example of the desired chaining:

await expect(contract.method(...))
  .to.be.revertedWithCustomError(...)
  .and.to.not.changeTokenBalance(...);

As a temporary workaround, you can create separate assertions for each async matcher:

const tx = contract.method(...);
await expect(tx).to.be.revertedWithCustomError(...);
await expect(tx).to.not.changeTokenBalance(...);

If you believe implementing chaining for async matchers would be valuable and beneficial for your testing workflows, please consider upvoting this issue to show your interest and support.

Search terms

No response

sherpya commented 1 year ago

I always known there was something strange in my tests :) perhaps multiple emits looks like can be chained, correct?

fvictorio commented 1 year ago

Yes, .emit is the only one that can be chained (and only to itself).

@schaable we should update the plugin's readme to add emit to the list of matchers that do not support chaining, and add a clarification saying "The only exception is chaining multiple .emit matchers" or something like that.