Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

You needed to import hre in the test code.

const hre = require("hardhat");
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda