testing – Using Fork on Hardhat in Testing: A Comprehensive Guide

hardhattesting

I am getting an error using a hardhat fork for testing.
I run the forked node with

"npx hardhat node –fork https://eth-mainnet.alchemyapi.io/v2/key –fork-block-number 13997235"

I am able to successfully query the node from a web3 application.
When I try to run tests with "npx hardhat test" it fails.

The test code is:

const { expect } = require("chai");
const { ethers, waffle } = require("hardhat");
const provider = waffle.provider;


it("test getting contract data from fork", async function () {
    WETH9 = await ethers.getContractFactory('WETH9')
    const WETH_address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
    weth = await WETH9.attach(WETH_address)
    console.log("rweth: " + await weth.decimals()); 
    return true;
  })  

The error is,

"Error: call revert exception (method="decimals()", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.5.0)"

Best Answer

Afaik hardhat test spins up their own test network, unless you specify it in the hardhat config.

Alternatively you should be able specify explicitly that hardhat should use your local hardhat node (e.g. http://localhost:8545) by adding --network localhost. See https://hardhat.org/hardhat-network/#running-stand-alone-in-order-to-support-wallets-and-other-software

Related Topic