PatrickAlphaC / hardhat-fund-me-fcc

82 stars 183 forks source link

FundMe.test.js Error #113

Open elliottmcknight opened 1 year ago

elliottmcknight commented 1 year ago

I am receiving an error when running the "yarn hardhat test" command. Once I run the command, I get this message:


    constructor
      ✓ sets the aggregator addresses correctly
    fund

      1) Fails if you don't send enough ETH

  1 passing (2s)
  1 failing

  1) FundMe
       fund
         Fails 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:31:46)

error Command failed with exit code 1.'''

Here is my FundMe.test.js code below:

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

describe("FundMe", async function() {
    let fundMe
    let deployer
    let MockV3Aggregator
    beforeEach(async function() {
        // deploy our FundMe contract
        // using Hardhat-deploy
        // const accountZero = accounts[0]
        // const accounts = await ethers.getSigners()
        deployer = (await getNamedAccounts()).deployer
        await deployments.fixture(["all"])
        fundMe = await ethers.getContract("FundMe", deployer)
        MockV3Aggregator = await ethers.getContract(
            "MockV3Aggregator",
            deployer
        )
    })

    describe("constructor", async function() {
        it("sets the aggregator addresses correctly", async function() {
            const response = await fundMe.getPriceFeed()
            assert.equal(response, MockV3Aggregator.address)
        })
    })

    describe("fund", async function() {
        it("Fails if you don't send enough ETH", async function() {
            await expect(fundMe.fund()).to.be.revertedWith(
                "You need to spend more ETH!"
            )
        })
    })
})
ccurves commented 1 year ago

@elliottmcknight What version of chai are you using? Did you install the @nomicfoundation/hardhat-toolbox package as suggested in the hardhat documentation?

hendythee commented 1 year ago

Hi I am also stuck on this section but getting a different error message as follows:

FundMe
    constructor
      ✔ sets the aggregator addresses correctly
    fund
      1) Fails if you don't send enough ETH

  1 passing (727ms)
  1 failing

  1) FundMe
       fund
         Fails if you don't send enough ETH:
     AssertionError: Expected transaction to be reverted with reason 'more ETH is needed!', but it reverted with reason 'Please send more ETH!'
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at runNextTicks (node:internal/process/task_queues:64:3)
      at listOnTimeout (node:internal/timers:533:9)
      at processTimers (node:internal/timers:507:7)
      at Context.<anonymous> (test/unit/FundMe.test.js:30:13)

any idea to solve this?

hendythee commented 1 year ago

Hi I am also stuck on this section but getting a different error message as follows:

FundMe
    constructor
      ✔ sets the aggregator addresses correctly
    fund
      1) Fails if you don't send enough ETH

  1 passing (727ms)
  1 failing

  1) FundMe
       fund
         Fails if you don't send enough ETH:
     AssertionError: Expected transaction to be reverted with reason 'more ETH is needed!', but it reverted with reason 'Please send more ETH!'
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at runNextTicks (node:internal/process/task_queues:64:3)
      at listOnTimeout (node:internal/timers:533:9)
      at processTimers (node:internal/timers:507:7)
      at Context.<anonymous> (test/unit/FundMe.test.js:30:13)

any idea to solve this?

I solved this by omitting 'await', so it is written as follows:

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

I have no idea why it solves the problem? but I got the idea of deleting 'await' from the the other issues thread.

zeeshan615 commented 1 year ago

i am getting: 0 passing (3s) 1 failing

1) FundMe "before each" hook for "sets the aggregator addresses correctly": TypeError: Cannot read properties of undefined

VINURIRODRIGO commented 1 year ago

To resolve the issue, you can follow these steps:

  1. Open the hardhat.config.js file in your project directory.
  2. Find the line of code that includes require("@nomiclabs/hardhat-waffle").
  3. Replace it with require("@nomicfoundation/hardhat-chai-matchers").
VanVasquez commented 1 year ago

Good Morning!!. I think I found a way to solve this issue...

I also got this error:

van123@Van-PC:~/blockchain-tuts/hardhat-fundme$ yarn hardhat test
yarn run v1.22.15
$ /home/van123/blockchain-tuts/hardhat-fundme/node_modules/.bin/hardhat test

  FundMe
    constructor
      ✔ sets the aggregator address correctly
    fund
      1) fails if we dont send enough eth

  1 passing (3s)
  1 failing

  1) FundMe
       fund
         fails if we dont send enough eth:
     AssertionError: Expected transaction to be reverted with Not enough eth!, but other exception was thrown: Error: VM Exception while processing transaction: reverted with reason string 'Not enough eth'

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I have been searching for ways to solve this and found that removing "await" does solve it but it is really not right since it returns promise.

describe('fund', () => {
    it('fails if we dont send enough eth', async () => {
      expect(fundMe.fund()).to.be.revertedWith('Not enough eth!');
    });
  });

the right thing is still to add await, but it has to match the reverted message from the solidity!!, you should check your FundMe.sol file if it matches the reverted message

FundMe.test.js:

describe('fund', () => {
    it('fails if we dont send enough eth', async () => {
      await expect(fundMe.fund()).to.be.revertedWith('Not enough eth');
    });
  });

FundMe.sol

function fund() public payable {
    require(
      msg.value.getConversionRate(priceFeed) >= MINIMUM_USD,
      'Not enough eth'
    );
    addressToAmountFunded[msg.sender] += msg.value;
    funders.push(msg.sender);
  }

hope this helps

GIRISHNP commented 1 year ago

@VINURIRODRIGO thanks it worked