tito
tito

Reputation: 93

TypeError: Cannot read properties of undefined (reading 'getContractFactory') when testing contract

First question so bare with me if it is not very clear, but I'll try my best.

I am currently running through a youtube video to test my contract with hardhat, ethers, and waffle (https://www.youtube.com/watch?v=oTpmNEYV8iQ&list=PLw-9a9yL-pt3sEhicr6gmuOQdcmWXhCx4&index=6).

Here is the contract:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract MyContract is ERC721 {

  constructor(string memory name, string memory symbol) 
    ERC721(name, symbol) {

    }
  
}

And here is test.js:

const { expect } = require('chai');

describe("MyContract", function() {
  
  it("should return correct name", async function() {
    const MyContract = hre.ethers.getContractFactory("MyContract");
    const myContractDeployed = await MyContract.deploy("MyContractName", "MCN");
    await myContractDeployed.deployed();
    
    expect(await myContractDeployed.name()).to.equal("MyContractName");
  });
});

when I run "npx hardhat test" in the terminal it returns:

MyContract
    1) should return correct name


  0 passing (7ms)
  1 failing

  1) MyContract
       should return correct name:
     TypeError: Cannot read properties of undefined (reading 'getContractFactory')
      at Context.<anonymous> (test\test.js:7:35)
      at processImmediate (node:internal/timers:464:21)

My code matches the one from the video, and I am having a tough time understanding why I am getting a TypeError here. Any guidance is much appreciated!

EDIT:

I somehow fixed it, I dont understand how exactly it fixed it but it did. Instead of just installing

npm install @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

I installed

npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

Then the terminal printed

npm WARN idealTree Removing dependencies.@nomiclabs/hardhat-waffle in favor of devDependencies.@nomiclabs/hardhat-waffle
npm WARN idealTree Removing dependencies.ethereum-waffle in favor of devDependencies.ethereum-waffle
npm WARN idealTree Removing dependencies.@nomiclabs/hardhat-ethers in favor of devDependencies.@nomiclabs/hardhat-ethers
npm WARN idealTree Removing dependencies.ethers in favor of devDependencies.ethers

then I removed the hre in front of ethers.getContractFactory("MyContract") and it worked! If anyone would like to explain why this might have fixed it I'd be happy to read it, otherwise I am moving on.

Upvotes: 9

Views: 14356

Answers (4)

Yakup Akiniyazov
Yakup Akiniyazov

Reputation: 1

I think problem with your expect code. Normally it is like :

expect(await myContractDeployed.name()).to.be.equal("MyContractName");

In Hardhat, the --dev flag is used when installing libraries using the npm or yarn package managers to indicate that the library being installed is a development dependency.

A development dependency is a package that is only needed during the development or testing phase of a project and is not required for the final production deployment. By marking a package as a development dependency, it is not included in the final production build, making the build smaller and faster.

Upvotes: 0

Abdulhakim
Abdulhakim

Reputation: 758

Sometimes it is because any of these dependencies below missing. Especially if you are using dotenv file and forgetting to import it. So, put these import statements on your hardhat.config or truffle.config file:

require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-ethers");
require("dotenv").config();

Upvotes: 1

priyanshu bindal
priyanshu bindal

Reputation: 346

Add the following code snippet at the top of your hardhat.config.js file

require("@nomiclabs/hardhat-waffle");

Upvotes: 12

Vighnesh Velayudhan
Vighnesh Velayudhan

Reputation: 21

You needed to import hre in the test code.

const hre = require("hardhat");

Upvotes: -1

Related Questions