defi-wonderland / smock

The Solidity mocking library
MIT License
321 stars 39 forks source link

TypeError: Cannot read properties of undefined (reading 'events') #163

Closed carlomigueldy closed 1 year ago

carlomigueldy commented 1 year ago

Describe the bug

After mock contract deployment, it just throws the exception.

Reproduction steps

  1. Create a very basic contract
  2. Initialize a simple test suite and use @defi-wonderland/smock later version with Hardhat latest version
  3. Deploy the mock contracts on the test suite
  4. Run the unit test and observe it failing throwing the exception Cannot read properties of undefined (reading 'events')

Expected behavior A clear and concise description of what you expected to happen.

It should throw exception after mock contract deployment in before or beforeEach hooks The exception to throw: Cannot read properties of undefined (reading 'events')

Screenshots If applicable, add screenshots to help explain your problem.

Screenshot from 2022-10-06 11-40-50

Screenshot from 2022-10-06 11-40-17

System Specs:

Additional context Add any other context about the problem here.

wei3erHase commented 1 year ago

yoo @carlomigueldy, code looks solid to me, have u tried a beforeAll instead of before? blz submit full SavePakistanSmock.ts to give a proper-er advice

carlomigueldy commented 1 year ago

yoo @carlomigueldy, code looks solid to me, have u tried a beforeAll instead of before? blz submit full SavePakistanSmock.ts to give a proper-er advice

gm bro, unfortunately there is not beforeAll available only before and beforeEach and so I tried beforeEach and still throwing the same exception

Here be the entire test suite as you ask:

import { MockContract, MockContractFactory, smock } from "@defi-wonderland/smock";
import { SavePakistan, SavePakistan__factory } from "../typechain-types";
import chai, { expect } from "chai";
import { TokenVariant } from "../utils";
import { BigNumber } from "ethers";

chai.should();
chai.use(smock.matchers);

describe("Smock: SavePakistan", () => {
  let savePakistanFactory: MockContractFactory<SavePakistan__factory>;
  let savePakistan: MockContract<SavePakistan>;

  before(async () => {
    savePakistanFactory = await smock.mock<SavePakistan__factory>("SavePakistan");

    savePakistan = await savePakistanFactory.deploy();
    console.log("savePakistanMock.address", savePakistan.address, "...");
  });

  it("should be cool", async () => {
    console.log("COOL");
  });

  // it("should make it", async () => {
  //   console.log("savePakistanMock.address", savePakistan.address, "...");
  //   savePakistan.mintByPayingEth
  //     .whenCalledWith(TokenVariant.HygieneKit, BigNumber.from("1"))
  //     .returns(undefined);
  // });

  // it("should mock the shit out of it", async () => {
  //   savePakistanMock.USDC_ADDR.returns("carlo");

  //   const usdcAddr = await savePakistanMock.USDC_ADDR();
  //   console.log("usdcAddr", usdcAddr);
  // });

  // it("should call getters", async () => {
  //   expect(await mock.count()).to.equal(1);
  // });

  // it("should call methods", async () => {
  //   await mock.add(10);
  //   expect(await mock.count()).to.equal(11);
  // });

  // it("should be able to override returns", async () => {
  //   mock.count.returns(123);
  //   expect(await mock.count()).to.equal(123);
  // });

  // it("should be able to override a function that reverts", async () => {
  //   mock.doRevert.returns(true);
  //   expect(await mock.doRevert()).to.equal(true);
  // });

  // it("should be able to check function calls", async () => {
  //   await mock.add(10);
  //   expect(mock.add).to.be.calledOnceWith(10);
  // });
});
wei3erHase commented 1 year ago

Copied and run, it works cool.

image

Because of our repo structure, I had to change the import to:

import { SavePakistan, SavePakistan__factory } from "../../typechained";

Idk if it had something to do...

carlomigueldy commented 1 year ago

Copied and run, it works cool. image

Because of our repo structure, I had to change the import to:

import { SavePakistan, SavePakistan__factory } from "../../typechained";

Idk if it had something to do...

damn, can you share me your repo + dependencies? lol

wei3erHase commented 1 year ago

Sure fren, i'd recommend it to bootstrap and boost-up any project: https://github.com/defi-wonderland/solidity-hardhat-boilerplate

carlomigueldy commented 1 year ago

Sure fren, i'd recommend it to bootstrap and boost-up any project: https://github.com/defi-wonderland/solidity-hardhat-boilerplate

The suggested template/boilerplate for Hardhat works very well!

So I feel safe to close this issue as this template works the best :+1:

yehted commented 1 year ago

I'm having this issue as well - @carlomigueldy did you ever figure out why the repo structure caused this error?

Edit: I joined the smock discord, and some others have seen this issue. They resolved it by upgrading hardhat to 2.12.0. That got me past this error.