Skip to content

Computing code coverage of your tests

Hardhat 3 has built-in support for code coverage, making it easy to see which parts of your contracts are tested. To use it, run

Terminal window
npx hardhat test --coverage

This will show you the combined coverage of all your tests.

You can also use the --coverage flag with individual tests or test subtasks. For example, this shows the coverage of a single Solidity test:

Terminal window
npx hardhat test solidity contracts/Counter.t.sol --coverage

Hardhat instruments your Solidity smart contracts by adding markers that it detects at runtime and uses to compute code coverage for your test runs.

This approach has several benefits:

  • Your code coverage won’t change with different solc versions
  • It works with optimized code

However, this comes with some side effects:

  • The bytecode of your contracts running in coverage mode is different and larger
  • The gas costs of your contracts when running in coverage mode are higher
  • The allowUnlimitedContractSize setting of edr-simulated networks is automatically set to true when using --coverage

The lcov format is shared among many programming languages to report code coverage results.

When you run Hardhat with --coverage, an lcov file with the results is stored in coverage/lcov.info.

Displaying the lcov results in Visual Studio Code

Section titled “Displaying the lcov results in Visual Studio Code”

You can use the Coverage Gutter VSCode extension to see the results of your code coverage within your contract files.

Install the extension and run the command “Coverage Gutters: Watch” (coverage-gutters.watchCoverageAndVisibleEditors).

Whenever you open a contract file after having run your tests with --coverage, you’ll see the results right next to the line numbers.