最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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 (;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.

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.

Share Improve this question edited Jan 8, 2022 at 2:12 tito asked Jan 8, 2022 at 1:35 titotito 931 gold badge1 silver badge6 bronze badges 3
  • The error means that hre.ethers is undefined and that’s why you can’t acces a property (because it has none). My guess is that something in your code has gone wrong prior to this function. – Casper Kuethe Commented Jan 8, 2022 at 1:41
  • It seemed to just be a problem with how the packages were installed, thanks for the comment though! – tito Commented Jan 8, 2022 at 2:13
  • please post your hardhat.config.js file – houman.sanati Commented Feb 3, 2022 at 15:53
Add a comment  | 

4 Answers 4

Reset to default 12

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

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

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();

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.

You needed to import hre in the test code.

const hre = require("hardhat");

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论