Solidity: Testing reverted assertions with waffle + chai doesn't work
Asked Answered
S

1

8

I'm testing a smart contract for an ERC721 token, that inherits from OpenZeppelin, using waffle and chai with NodeJS v14.18.0, but chai's reverted assertions don't seem to function as expected.

Dependencies:

  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.1",
    "@nomiclabs/hardhat-etherscan": "^2.1.0",
    "@nomiclabs/hardhat-waffle": "^2.0.1",
    "@typechain/ethers-v5": "^5.0.0",
    "@types/chai": "^4.2.14",
    "@types/mocha": "^8.2.0",
    "@types/node": "^14.14.14",
    "chai": "^4.2.0",
    "dotenv": "^8.2.0",
    "ethereum-waffle": "^3.2.1",
    "ethers": "^5.0.24",
    "hardhat": "^2.0.5",
    "hardhat-typechain": "^0.3.4",
    "ts-generator": "^0.1.1",
    "ts-node": "^9.1.1",
    "typechain": "^4.0.1",
    "typescript": "^4.1.3"
  },

Sample code:

  let genesisTokenHolders: Array<string>;

        beforeEach(async () => {
            genesisTokenHolders = new Array(230).fill('0x0000000000000000000000000000000000000000')
            genesisTokenHolders[1] = addr1.address;
            genesisTokenHolders[2] = addr2.address;
        })

        describe('airdrop genesis tokens', async () => {
            it('should fail when non-owner calls the function', async () => {
                
                let addGenesisHoldersTx = await myToken.addGenesisTokenHolderAddresses(genesisTokenHolders)
                await addGenesisHoldersTx.wait();
                    
                await expect(await myToken.connect(addr1).airdropTokensToGenesisHolders(1, 2)).to.be.revertedWith("Ownable: caller is not the owner");
            })
        
            it('should succeed when called by the owner', async () => {

                let addGenesisHoldersTx = await myToken.addGenesisTokenHolderAddresses(genesisTokenHolders)
                await addGenesisHoldersTx.wait();
 
                let airdropTx = await myToken.connect(deployer).airdropTokensToGenesisHolders(1,2);
                await airdropTx.wait();
            
                // addr1 should own tokenID 1, addr2 should own tokenId 2
                await expect(await myToken.ownerOf(1)).to.hexEqual(addr1.address)
                await expect(await myToken.ownerOf(2)).to.hexEqual(addr2.address)
                
                await expect(await myToken.ownerOf(1000)).to.be.reverted;
            })
        });

Despite using .to.be.revertedWith() and .to.be.reverted, the test suite is still throwing errors:

         airdrop genesis tokens
           should fail when non-owner calls the function:
     Error: VM Exception while processing transaction: revert Ownable: caller is not the owner

         airdrop genesis tokens
           should succeed when called by the owner:
     Error: VM Exception while processing transaction: revert ERC721: owner query for nonexistent token

Expected results: the tests should pass.

Sciolism answered 29/10, 2021 at 20:18 Comment(0)
E
35

Don't wait for the promise to be resolved within expect

Change from:

await expect(await myToken.ownerOf(1000)).to.be.reverted;

To:

await expect(myToken.ownerOf(1000)).to.be.reverted;
Ejectment answered 7/11, 2021 at 0:3 Comment(2)
Thank you! This is the exact rookie mistake we all made at one point...Frohne
I was doing this too, thought that the second syntax was only for catching events, but i guess its for evaluating once a tx has been 'mined' (for ex. events), ASWELL as reversions?Panne

© 2022 - 2024 — McMap. All rights reserved.