6

I'm running a test using waffle in hardhat.

import { assert, expect } from "chai"
.
.
.

  it("can only be changed through governance", async () => {
    await expect(box.store(55)).to.be.revertedWith("Nope, you bad")
  })

However when I run tests, I run into:

test/unit/testflow.test.ts:31:39 - error TS2339: Property 'revertedWith' does not exist on type 'Assertion'.

I have the package imported in my hardhat.config.ts

import "@nomiclabs/hardhat-waffle"

And I have the package installed:

package.json

    "@nomiclabs/hardhat-waffle": "^2.0.2",

What am I missing?

3 Answers 3

6

Pre June 2022

You need to also add ethereum-waffle to your package, not just the nomiclabs one @nomiclabs/hardhat-waffle. you need both

yarn add --dev ethereum-waffle

you just only need to install it, other than that in the hardhat.config.js file this will not be "require" or "import". menas in hardhat.config.js only this will be imported

require("@nomiclabs/hardhat-waffle")

Post Just 2022

Hardhat has released a new package named hardhat-toolbox to replace hardhat-waffle.

Install it with npm install --save-dev @nomicfoundation/hardhat-toolbox or yarn add --dev @nomicfoundation/hardhat-toolbox and then import it in your hardhat.config.js like this:

require('@nomicfoundation/hardhat-toolbox')

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: '0.8.9',
  paths: {  },
  networks: {  },
}

Once done, you'll be able to use revertWith like this:

 it('my contract reverting', async () => {
    await expect(myContract.method()).to.be.revertedWith(
      'My revert error message'
    )
  })
1
  • but why don't hardhat-waffle has the "revertedWith" ? although hardhat just replicated whole waffle into its package hardhat-waffle !!! Jul 20, 2022 at 10:37
4

Hardhat has released a new package named hardhat-toolbox to replace hardhat-waffle.

Install it with npm install --save-dev @nomicfoundation/hardhat-toolbox and then import it in your hardhat.config.js like this:

require('@nomicfoundation/hardhat-toolbox')

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: '0.8.9',
  paths: {  },
  networks: {  },
}

Once done, you'll be able to use revertWith like this:

 it('my contract reverting', async () => {
    await expect(myContract.method()).to.be.revertedWith(
      'My revert error message'
    )
  })
1
  • 1
    Awesome, updated my answer with this. Jul 25, 2022 at 15:51
3

I was experiencing this issue as well and most online help weren't addressing the underlying cause.

You also have to add the following to your test files to use event emitters from chai matchers:

const { solidity } = require('ethereum-waffle')
const chai = require('chai');
chai.use(solidity);

You can find a more detailed example and explanation here:

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.