NomicFoundation / hardhat

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

Impossible to catch a reverted error with .to.be.reverted #5486

Closed YannSuissa closed 1 week ago

YannSuissa commented 2 weeks ago

Hi,

I can't catch a reverter error for the Onlyowner modifier from Openzepplin :

ProviderError: Error: VM Exception while processing transaction: reverted with custom error 'OwnableUnauthorizedAccount("0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC")'

I tried without success:

expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWithCustomError(nft_contract, "OwnableUnauthorizedAccount").withArgs("0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC");
expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWithCustomError(nft_contract, "OwnableUnauthorizedAccount")
expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWithCustomError(nft_contract, "/OwnableUnauthorizedAccount.*/")
expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWithCustomError(nft_contract, "/OwnableUnauthorizedAccount(.*)/")
expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.reverted
expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWith("OwnableUnauthorizedAccount")
expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWith("/OwnableUnauthorizedAccount.*/")
expect(await nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWith("/OwnableUnauthorizedAccount(.*)/")

In openzepplin it's this kind of error :

error OwnableUnauthorizedAccount(address account);
...
if (owner() != _msgSender()) {
  revert OwnableUnauthorizedAccount(_msgSender());
}

here are my versions :

"@nomicfoundation/hardhat-chai-matchers": "^2.0.7",
"@nomicfoundation/hardhat-ethers": "^3.0.6",
"@nomicfoundation/hardhat-ignition": "^0.15.5",
"@nomicfoundation/hardhat-ignition-ethers": "^0.15.5",
"@nomicfoundation/hardhat-network-helpers": "^1.0.11",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.8",
"@typechain/ethers-v6": "^0.5.1",
"@typechain/hardhat": "^9.1.0",
"chai": "4",
"ethers": "^6.13.1",
"hardhat": "^2.22.6",
"hardhat-gas-reporter": "^2.2.0",
"solidity-coverage": "^0.8.12",
"typechain": "^8.3.2" 
Varadiell commented 1 week ago

Hi there, you should try like this:

await expect(nft_contract.connect(addrs[2]).set_sale_step(1)).to.be.revertedWithCustomError(nft_contract, "OwnableUnauthorizedAccount").withArgs("0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC");

Your await is most likely not at the right place.

YannSuissa commented 1 week ago

Thanks all my test were wrong because of the await ...