How would you test the following function with solidity v0.7 and hardhat v2.3.3?

function myFunction(int8 _num) public {
        require(_num > 5, "Num should be bigger than 5");

I've tried:

try {
         await contract.myFunction(3);
} catch(error) {
         expect(error).to.equals("Num should be bigger than 5");

And also:

expect.fail(await contract.myFunction(3));

But I always get:

Error: VM Exception while processing transaction: revert Num should be bigger than 5

Which seems to me the Virtual Machine breaking off with the test failing in the spot, without even having the chance to catch the error.

  • why are you using int8 and not just int?
    – Majd TL
    Commented Jun 23, 2021 at 11:28
  • possible values for that var are 1, 2 and 3 @MajdTL. That's why I'm using int8. Not sure it would safe gas, thou. I did not research that.
    – ntonnelier
    Commented Jun 23, 2021 at 11:56
  • no it will cost you more gas, because the default is int and you changing it to int8
    – Majd TL
    Commented Jun 23, 2021 at 13:11
  • ethereum.stackexchange.com/questions/3067/…
    – Majd TL
    Commented Jun 23, 2021 at 13:35

1 Answer 1


When I test a function for revert I do the following in my javascript test file (e.g. myContractTest.js):

const { expect } = require('chai');
const { ethers } = require('hardhat');
const { BigNumber } = require('ethers');

describe('Test contract', () => {
    it('deploy the smart contract and reverts', async () => {
        const MyContract = await ethers.getContractFactory('MyContract');
        const contractInstance = await MyContract.deploy(<add something if you have parameters in the constructor>);
        await expect(contractInstance.myFunction(BigNumber.from('6')))
        .to.be.revertedWith('Num should be bigger than 5');

Then Run

npx hardhat test --network hardhat ./test/myContractTest.js

Documentation here: https://ethereum-waffle.readthedocs.io/en/latest/matchers.html#revert-with-message

  • thanks @Majd TL, but that still crashes the VM. Same error. Btw, not sure I mentioned it, but it's an async function.
    – ntonnelier
    Commented Jun 23, 2021 at 11:54
  • @ntonnelier which solidity version are you using , and which hardhat version. are you changing a state inside that function or it is just a call data function?
    – Majd TL
    Commented Jun 23, 2021 at 13:14
  • it's a payable function and it changes data in storage (although after the require that throws the error here). I'm using solidity 0.7 and hardhat 2.3.3 @MajdTL
    – ntonnelier
    Commented Jun 23, 2021 at 13:23
  • and you dont get any error when testing that in remix?
    – Majd TL
    Commented Jun 23, 2021 at 13:30
  • I updated my answer there was an error in the message idk if you noticed it. @ntonnelier
    – Majd TL
    Commented Jun 23, 2021 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.