Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Invalid Chai property: revertedWith

I want to test my contract FundMe.sol but how it takes off FundMe.test.js an error pops up this

My console

FundMe
    constructor
Development network detected! Deploying mocks...
      ✓ Should set the aggregator addresses correctly
    fund

      1) Should fail if you don't send enough ETH


  1 passing (977ms)
  1 failing

  1) FundMe
       fund
         Should fail if you don't send enough ETH:
     Error: Invalid Chai property: revertedWith
      at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:78:17)
      at Context.<anonymous> (test/unit/FundMe.test.js:29:46)

the file where the test is written to the file FundMe.sol

FundMe.test.js

const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect, revertedWith } = require("chai")

describe("FundMe", async function() {
    let fundMe
    let deployer
    let mockV3Aggregator
    beforeEach(async function() {
        deployer = (await getNamedAccounts()).deployer
        await deployments.fixture(["all"]) // deploy all contracts using deployment.fixture()
        fundMe = await ethers.getContract("FundMe", deployer)
        mockV3Aggregator = await ethers.getContract(
            "MockV3Aggregator",
            deployer
        )
    })

    describe("constructor", async function() {
        it("Should set the aggregator addresses correctly", async function() {
            const response = await fundMe.priceFeed()
            assert.equal(response, mockV3Aggregator.address)
        })
    })

    describe("fund", async function() {
        it("Should fail if you don't send enough ETH", async function() {
            await expect(fundMe.fund()).to.be.revertedWith("You need to spend more ETH!")
        })
    })
})

What am I doing wrong?

like image 717
Paweł Stach Avatar asked Nov 15 '25 03:11

Paweł Stach


2 Answers

You just need to install hardhat-chai-matchers like this:

$ yarn add --dev @nomicfoundation/hardhat-chai-matchers

Or

npm install --save-dev @nomicfoundation/hardhat-chai-matchers

And then add this line to your hardhat.config.js:

require("@nomicfoundation/hardhat-chai-matchers");

More info: https://hardhat.org/hardhat-chai-matchers/docs/overview

like image 74
Lulox Avatar answered Nov 17 '25 18:11

Lulox


You need to add ethereum-waffle to your packages. With @nomiclabs/hardhat-waffle being installed it is not working!

yarn add --dev ethereum-waffle

Once done, you'll be able to use revertedWith.

Recent hardhat's new package hardhat-toolbox has replaced few basic packages here

like image 21
Black Candy Avatar answered Nov 17 '25 18:11

Black Candy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!