I'm currently working on a project where I need to perform integration testing involving a third-party contract. This contract is part of a library that I've included in my project as a git submodule located in the /lib directory. My goal is to avoid cluttering my /contracts folder with the entire library's contents.

For my tests, I need to deploy this third-party contract to interact with it. I'm using Hardhat for my development environment, which has been great for local testing so far. However, I'm unsure how to proceed with deploying and interacting with this third-party contract without moving it into my /contracts folder.

My Questions:

  1. Is there a way to deploy and test against this third-party contract on a local blockchain (e.g., Hardhat Network) without copying the contract into my /contracts folder? Ideally, I'd like to keep my project structure clean and maintain the separation of third-party contracts.
  2. If direct deployment on the Hardhat Network is not feasible in this manner, am I forced to use TestNets for this purpose? For example, deploying the third-party contract to a TestNet first and then running my integration tests against that deployed contract.

Here's an example of what my project structure looks like:

├── contracts/
│   └── MyContract.sol
├── lib/
│   └── third-party-contracts/ (git submodule)
│       └── ThirdPartyContract.sol
├── test/
│   └── MyContract.test.js
└── hardhat.config.js

I'm happy to use either ethers.js or Viem for the test implementation. Any guidance on how to approach this situation would be greatly appreciated. Thank you!

1 Answer 1


If the third-party party contract is already deployed on a mainnet or if you deploy it on a testnet, you can then fork the network state with the third-party contract & run your own development network.

Example: Forking EVM-compatible mainnet with Hardhat.

  • may be integration-testing is the wrong term? (i've seen different definition of it). My test is closer to a unittest I suppose, I want to write tests about how MyContract.sol interact with a ThirdPartyContract.sol. Running test against a TestNet seems sub-optimal. For went for the @nomicfoundation/hardhat-foundry plugin, using forge test for those tests
    – codekoriko
    Feb 14 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.