Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

281
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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

require("@nomiclabs/hardhat-waffle");
about 4 years ago · Juan Pablo Isaza Relatório

0

You needed to import hre in the test code.

const hre = require("hardhat");
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda