Search code examples
testingdeploymentconfigsolidityhardhat

How do I use different config for testing vs. deployment hardhat solidity?


Right now, when using hardhat, I have a different config for testing and deployment. Currently I am changing the file name depending on whether I am testing or deploying. This does not seem optimal/correct.

Does anyone know a way I can specify which to use? Or even better, a way to specify in the config testing vs deployment?

Testing config:

require("@nomiclabs/hardhat-waffle");
/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.0",
};

Deployment config:

 * @type import('hardhat/config').HardhatUserConfig
 */
require('dotenv').config();
require("@nomiclabs/hardhat-waffle")
const {API_URL, METAMASK_PRIVATE_KEY} = process.env;
module.exports = {
  solidity: "0.8.0",
  defaultNetwork: "rinkeby",
  networks: {
    hardhat: {},
    rinkeby: {
      url: API_URL,
      accounts: [`0x${METAMASK_PRIVATE_KEY}`]
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
};

I guess really I just want to ignore the "networks" field when testing...


Solution

  • I realized the only difference between these configs was the network flag, for testing I wanted to use the hardhat network, and deployment, rinkeby.

    Changing the default network param to:

    defaultNetwork: "Hardhat"
    

    allowed me to run tests on the hardhat network using the npx hardhat test command.

    Then deploying I can use:

    npx hardhat run --network rinkeby scripts/deploy.js