Troubleshooting
This page covers common issues and solutions when working with solidity-coverage
.
Missing or Unexpected Coverage?
If you're seeing incorrect coverage reports or files are missing, it might be due to outdated artifacts or cache. Follow these steps to ensure a clean run:
-
Clean artifacts and cache:
npx hardhat clean
-
Recompile your contracts:
npx hardhat compile
-
Run coverage again:
npx hardhat coverage
TypeScript Compilation Errors
If you are using TypeScript and encounter compilation errors only when running the coverage task, it might be a type-checking issue that is safe to ignore during the coverage run. You can bypass this by setting the TS_NODE_TRANSPILE_ONLY
environment variable:
TS_NODE_TRANSPILE_ONLY=true npx hardhat coverage
Weird Test Failures or Plugin Conflicts
solidity-coverage
needs to configure the HardhatEVM provider early in the task lifecycle to work correctly. Sometimes, other plugins or custom extendEnvironment
hooks in your configuration can interfere with this process, leading to unexpected test failures.
To minimize these conflicts, you can force the coverage plugin to configure the provider earlier by setting the SOLIDITY_COVERAGE
environment variable:
SOLIDITY_COVERAGE=true npx hardhat coverage
This is particularly useful when using plugins like hardhat-viem
.
Detecting solidity-coverage
from Another Task
If you are writing another Hardhat plugin or task, it can be useful to know if solidity-coverage
is currently running. The plugin sets a boolean variable on the global Hardhat Runtime Environment (HRE) for this purpose.
You can check for its presence in your task or plugin logic:
if (hre.__SOLIDITY_COVERAGE_RUNNING === true) {
// Coverage is running, maybe disable gas reporting...
} else {
// Coverage is not running.
}