Running OpenZeppelin Contracts tests outside hardhat

Is there a way to run (for example) ERC20 smart contract tests outside hardhat? Like for example on Sepolia? With version 4.7 I can do this. With newer not.

When I try, I get the OnlyHardhatNetworError.

Is there a way around this?

Please be more specific with your question and provide more detail. See:

Yes you can run the tests on any network.

For that you would need to configure the hardhat with desired network.

For example :

Put this inside the module.exports in hardhat config file.

networks: {

    sepolia: {
      url: `https://sepolia.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,//any rpc url will work
      accounts: [process.env.PRIVATE],
    },

}

And then you can run the test command like this

npx hardhat test ./test/TestFileName.js --network sepolia

This will run you test file on sepolia testnet, but you should also be aware that this will need your wallet to be funded with the Sepolia ETH.

This are the steps i took:

sepolia: {
url: https://sepolia.infura.io/v3/${INFURA_TOKEN},
accounts: {
mnemonic: "some secret mnemonic"
},
timeout: 120000,
},

  • I ran the following:

npx hardhat --show-stack-traces --network sepolia test test/token/ERC20/ERC20.test.js

  • and I got the following result (error, does not work):

Compiled 212 Solidity files successfully

Contract: ERC20
1) "before all" hook in "Contract: ERC20"
2) "after all" hook in "Contract: ERC20"

0 passing (474ms)
2 failing

  1. Contract: ERC20
    "before all" hook in "Contract: ERC20":
    OnlyHardhatNetworkError: This helper can only be used with Hardhat Network. You are connected to 'sepolia', whose identifier is 'Geth/v1.13.1-omnibus-3e6e9d8e/linux-amd64/go1.20.7'
    at checkIfHardhatNetwork (/home/krzychu/Develop/Aurora/github/openzeppelin-aurora/openzeppelin-contracts/node_modules/@nomicfoundation/hardhat-network-helpers/src/utils.ts:29:11)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at getHardhatProvider (/home/krzychu/Develop/Aurora/github/openzeppelin-aurora/openzeppelin-contracts/node_modules/@nomicfoundation/hardhat-network-helpers/src/utils.ts:40:3)
    at takeSnapshot (/home/krzychu/Develop/Aurora/github/openzeppelin-aurora/openzeppelin-contracts/node_modules/@nomicfoundation/hardhat-network-helpers/src/helpers/takeSnapshot.ts:20:20)
    at Context. (/home/krzychu/Develop/Aurora/github/openzeppelin-aurora/openzeppelin-contracts/hardhat/env-contract.js:29:20)

  2. Contract: ERC20
    "after all" hook in "Contract: ERC20":
    TypeError: Cannot read properties of undefined (reading 'restore')
    at Context. (hardhat/env-contract.js:35:24)
    at processImmediate (node:internal/timers:466:21)

  • but when I do the same, after I have done this:

git checkout release-v4.7

I am getting this (success, works as expected):

Compiled 272 Solidity files successfully (evm target: london).

Contract: ERC20
:heavy_check_mark: has a name (124ms)
:heavy_check_mark: has a symbol (118ms)
:heavy_check_mark: has 18 decimals (124ms)
set decimals
(...)

unfortunately it does not work this way... please see my added information... thanks!

Note that the tests configured in the OpenZeppelin Contracts repository are meant to be run on a Hardhat network. Running them on another network is not supported by default, and doing so may cause some tests to fail because they make use of Hardhat specific calls.

However, related the specific error that you are getting, the hardhat/env-contract.js script resets the Hardhat network between test suites in 5.0. You could try replacing that with the version from 4.x and see if it avoids the problem.

Thanks! It really helped a little. Now most of the tests fail something like this:

  at processImmediate (node:internal/timers:466:21)

I guess it it because hardhat delivers the response immedietely, but not Sepolia.
Do you know a way around this?
Thanks in advance!