Unable to set state variables during staging test on Goerli using Hardhat

Viewed 8

I am able to set variables during my unit tests on the local Hardhat Node, however the same test fails on Goerli.

This is the code for my test:

const { getNamedAccounts, deployments, ethers, network } = require("hardhat")
const { developmentChains, networkConfig } = require("../../helper-hardhat-config")
const { assert, expect } = require("chai")
const fs = require("fs")
const path = require("path")

developmentChains.includes(network.name)
    ? describe.skip
    : describe("Whoopy Staging Tests", function () {
          let whoopy,
              deployer,
              player,
              cloneAddress,
              clonedContract,
              txReceipt,
              tx,
              accountConnectedClone,
              manager,
              create

          const chainId = network.config.chainId

          beforeEach(async function () {

            accounts = await ethers.getSigners()
            deployer = accounts[0]
            player = accounts[1]
            await deployments.fixture(["all"])

            const dir = path.resolve(
                __dirname,
                "/Users/boss/hardhat-smartcontract-whoopy/artifacts/contracts/Whoopy.sol/Whoopy.json"
            )

            const file = fs.readFileSync(dir, "utf8")
            const json = JSON.parse(file)
            const abi = json.abi

            whoopy = await ethers.getContract("Whoopy", deployer)
            manager = await ethers.getContract("VRFv2SubscriptionManager", deployer)
            wf = await ethers.getContract("WhoopyFactory", deployer)

            tx = await (await wf.connect(player).createClone(player.address))
            txReceipt = await tx.wait(1)


            cloneAddress = await txReceipt.events[1].args._instance
            clonedContract = new ethers.Contract(cloneAddress, abi, player)
            accountConnectedClone = clonedContract.connect(player)
        })

        describe("Whoopy Tests", function () {
            beforeEach(async function () {
                create = await accountConnectedClone.createWhoopy(
                    "Test",
                    1,
                {
                    value: ethers.utils.parseUnits("10000000000000000", "wei"),
                    gasLimit: 3000000
                })
                console.log(create)
            })
            it("creates Whoopy correctly", async function () {

                const whoopyName = await accountConnectedClone.getWhoopyName()
                const num = await accountConnectedClone.getNum()

                expect(whoopyName).to.equal("Test")
                expect(num).to.equal(1)
            })
        })

Here is the function I am testing:

  function createWhoopy( //to be created only once 
        string memory s_whoopyName,
        uint256 s_num,
    ) public payable restricted {
        whoopyName = s_whoopyName;
        num = s_num;
}

Seems pretty straight forward but don't know why it's not working. My accounts have enough test eth and goerli is correctly set up in Hardhat config. The test reverts as follows:

AssertionError: expected '' to equal 'Test'
      + expected - actual

      +Test

Any guidance would be appreciated! Thanks!

1 Answers

I figured it out. It was something stupid (as I thought it would be). I forgot to put await create.wait(6) which was needed to wait for the blocks to mine the transaction.

It went through on Hardhat because Hardhat mines blocks immediately, but since Goerli is a live Testnet you need to wait for the transaction to be mined.

Related