You can run your tests in parallel by using the
$ npx hardhat test --parallel
parallel: true in the
mocha section of
hardhat.config, which will enable parallelism for plugins that don't have a
--parallel flag, like
Most of the time, running your tests serially or in parallel should produce the same results, but there are some scenarios where tests run in parallel will behave differently:
- In serial mode all the test files share the same instance of the Hardhat Runtime Environment, but in parallel mode this is not always the case. Mocha uses a pool of workers to execute the tests, and each worker starts with its own instance of the HRE. This means that if one test file deploys a contract, then that deployment will exist in some of the other test files and it won't in others.
.onlymodifier doesn't work in parallel mode. As an alternative, you can use
--grepto run specific tests.
- Because parallel mode uses more system resources, the duration of individual tests might be longer, so there's a chance that some tests start timing out for that reason. If you run into this problem, you can increase the tests timeout in the Mocha section of your Hardhat config or using
this.timeout()in your tests.
- The order in which tests are executed is non-deterministic.
There are some other limitations related to parallel mode. You can read more about them in Mocha's docs. And if you are running into some issue when using parallel mode, you can check their Troubleshooting parallel mode section.