Run hardhat tests on a testnet

contract-developmenthardhatsoliditytesttestnets

I want to run my tests on testnet, mainly because I need to test chainlink VRF. How can I do that?

this is my hardhat.config.js:

require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-waffle");
require('dotenv').config()

const INFURA_URL = "https://rinkeby.infura.io/v3/" + process.env.INFURA_API_KEY;
const owner = process.env.PK;
const alice = process.env.ALICE;
const bob = process.env.BOB;

module.exports = {
  networks: {
    hardhat:{},
    rinkeby: {
      url: INFURA_URL,
      accounts: [`0x${owner}`, `0x${alice}`, `0x${bob}`]
    }
  },
  solidity: {
    version: "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
};

I'm having this error after running npx hardhat test --network rinkeby:

      "before each" hook for "Should set the right owner":
    Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

This is the code part where is the error:
nft.test.js

beforeEach(async () => {
    NFT = await ethers.getContractFactory("NFT");
    nft = await NFT.deploy(
      initURI,
      secretURI,
      linkToken,
      vrfCoordinator,
      keyHash,
      linkFee
    );
    await nft.deployed();
    [owner, alice, bob, _] = await ethers.getSigners();
  });

Best Answer

Make sure your accounts have balances. And you can config the timeout depends on each network by adding a timeout property in milliseconds (https://hardhat.org/config/#json-rpc-based-networks)

example:

rinkeby: {
  url: INFURA_URL,
  accounts: [`0x${owner}`, `0x${alice}`, `0x${bob}`],
  timeout: 60000
}
Related Topic