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.