Skip to main content

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

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.revertedWith('Num should be bigger than 5');
    });
});

Then Run

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

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

wrong error message
Source Link
Majd TL
  • 3.3k
  • 3
  • 18
  • 36

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.revertedWith('no'Num Grant'should be bigger than 5');
    });
});

Then Run

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

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.revertedWith('no Grant');
    });
});

Then Run

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

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.revertedWith('Num should be bigger than 5');
    });
});

Then Run

npx hardhat test --network hardhat ./test/myContractTest.js
Source Link
Majd TL
  • 3.3k
  • 3
  • 18
  • 36

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.revertedWith('no Grant');
    });
});

Then Run

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