Search code examples
testingsoliditychaihardhat

before, beforeEach does not execute in hardhat test


This code is for testing a Silent Auction smart contract written in hardhat.

When I type everything separately into the it{...} blocks, the test are passing. When using before, and beforeEach to simplify the code it doesnt work.

Error message: "ReferenceError: silentAuction is not defined"

Can somebody tell me which part I do wrong?

const { BigNumber } = require("ethers");
const { ethers } = require("hardhat");

describe("SilentAuction", function () {

  before(async function () {
    const SilentAuction = await ethers.getContractFactory("SilentAuction");
  })

  beforeEach(async function () {
    const silentAuction = await SilentAuction.deploy();
    await silentAuction.deployed();
  })

  describe("- setItem function tests", function (){
    it("Owner should be able to set an item for bid", async function () {
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();
      await silentAuction.setItem("SAMPLE", 100, 300);

      expect((await silentAuction.items(0)).name).to.be.equal("SAMPLE");
      expect((await silentAuction.items(0)).highestPrice.toString()).to.be.equal('100');
      expect((await silentAuction.items(0)).successLimit.toString()).to.be.equal('300');
    });

    it("Not owner cannot call function", async function () {
      const [owner, addr1] = await ethers.getSigners();
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();

      await expect(silentAuction.connect(addr1).setItem("SAMPLE", 100, 300)).to.be.reverted;
    });
  });

  describe("- bid function tests", function (){
    it("Everyone should place a bid", async function () {
      const [owner, addr1] = await ethers.getSigners();
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();

      await silentAuction.connect(owner).setItem("SAMPLE", 100, 300);
      await silentAuction.connect(addr1).bid(200);
      expect((await silentAuction.items(0)).highestPrice.toString()).to.be.equal('200');
    })

    it("Should not replace smaller bids than the recent highest", async function () {
      const [owner, addr1] = await ethers.getSigners();
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();

      await silentAuction.connect(owner).setItem("SAMPLE", 100, 300);
      await silentAuction.connect(addr1).bid(50);
      expect((await silentAuction.items(0)).highestPrice.toString()).to.be.equal('100');
    })
  })
});

Solution

  • silentAuction is a constant which is defined in the scope of before or beforeEach function, not in the scope of whole tests.

    If you want to use such variable in the whole tests, you should do something like this:

    describe('foo' () => {
      let silentAuction;
    
      beforeEach(() => {
         silentAuction = ...
      })
    })
    
    

    Also the reason you get reference error is that every test function can't find such variable within its or outer scope then it raises Reference Error.