Code coverage for Solidity smart-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
cgewecke 1cd0477048 Fix README options table 3 years ago
.circleci Update parser to 0.10.1 and re-enable zeppelin e2e (#585) 4 years ago
docs Add solcOptimizerDetails option (#683) 3 years ago
lib Add solcOptimizerDetails option (#683) 3 years ago
plugins Add solcOptimizerDetails option (#683) 3 years ago
scripts Use internal visibility for fn level hash method defs (#660) 3 years ago
test Add solcOptimizerDetails option (#683) 3 years ago
.gitignore Docs for beta release (#415) 5 years ago
.npmignore Docs for beta release (#415) 5 years ago
BUIDLER_README.md Make statement and function coverage measurement optional (#538) 4 years ago
CHANGELOG.md Update changelong 0.7.18 3 years ago
HARDHAT_README.md Add new hook and make temporary contracts directory configurable (#664) 3 years ago
LICENSE Add MIT License 8 years ago
README.md Fix README options table 3 years ago
api.js Make api require-able, expose plugin utils (#445) 5 years ago
package.json 0.7.18 3 years ago
truffle-plugin.json Add simple E2E for Buidler (#438) 5 years ago
utils.js Add API documentation (#447) 5 years ago
yarn.lock Fix hardhat_reset (#667) and add unit tests (#681) 3 years ago

README.md

solidity-coverage

Gitter chat npm (tag) CircleCI codecov buidler

Code coverage for Solidity testing

coverage example

Install

$ npm install --save-dev solidity-coverage

Resources:

Truffle V5

Add this package to your plugins array in truffle-config.js (Truffle docs)

module.exports = {
  networks: {...},
  plugins: ["solidity-coverage"]
}

Run

truffle run coverage [command-options]

Hardhat

Beginning with v0.7.12, this tool supports Hardhat and runs directly on HardhatEVM.

Require the plugin in hardhat.config.js (Hardhat docs)

require('solidity-coverage')

Run

npx hardhat coverage [command-options]

(Additional Hardhat-specific info can be found here)

@openzeppelin/test-environment

OpenZeppelin have written their own coverage generation scripts for test-environment using the solidity-coverage API. A working example can be found at openzeppelin-contracts, here.

Usage notes:

  • Coverage runs tests a little more slowly.
  • Coverage launches its own in-process ganache server.
  • You can set ganache options using the providerOptions key in your .solcover.js config.
  • Coverage distorts gas consumption. Tests that check exact gas consumption should be skipped.
  • Contracts are compiled without optimization. Please report unexpected compilation faults to issue 417

Command Options

Option Example Description
file --file="test/registry/*.js" (Truffle) Filename or glob describing a subset of tests to run. (Globs must be enclosed by quotes and use globby matching patterns)
testfiles --testfiles "test/registry/*.ts" (Buidler) Test file(s) to run. (Globs must be enclosed by quotes and use globby matching patterns)
solcoverjs --solcoverjs ./../.solcover.js Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed)
network --network development Use network settings defined in the Truffle or Buidler config
temp* --temp build Caution Path to a disposable folder to store compilation artifacts in. Useful when your test setup scripts include hard-coded paths to a build directory. More...

* Advanced use

Config Options

Additional options can be specified in a .solcover.js config file located in the root directory of your project.

Example:

module.exports = {
  skipFiles: ['Routers/EtherRouter.sol']
};
Option Type Default Description
silent Boolean false Suppress logging output
client Object require("ganache-core") Useful if you need a specific ganache version.
providerOptions Object { } ganache-core options
skipFiles Array ['Migrations.sol'] Array of contracts or folders (with paths expressed relative to the contracts directory) that should be skipped when doing instrumentation.
measureStatementCoverage boolean true Computes statement (in addition to line) coverage. More...
measureFunctionCoverage boolean true Computes function coverage. More...
istanbulFolder String ./coverage Folder location for Istanbul coverage reports.
istanbulReporter Array ['html', 'lcov', 'text', 'json'] Istanbul coverage reporters
mocha Object { } Mocha options to merge into existing mocha config. grep and invert are useful for skipping certain tests under coverage using tags in the test descriptions.
coverageContractsTemp String .coverage_contracts Temporary folder location for instrumented contracts - Note that this directory will automatically be deleted when coverage completes.
onServerReady* Function Hook run after server is launched, before the tests execute. Useful if you need to use the Oraclize bridge or have setup scripts which rely on the server's availability. More...
onPreCompile* Function Hook run after filesystem and compiler configuration is applied, before the compiler is run. Can be used with the other hooks to be able to generate coverage reports on non-standard / customized directory structures, as well as contracts with absolute import paths. More...
onCompileComplete* Function Hook run after compilation completes, before tests are run. Useful if you have secondary compilation steps or need to modify built artifacts. More...
onTestsComplete* Function Hook run after the tests complete, before Istanbul reports are generated. More...
onIstanbulComplete* Function Hook run after the Istanbul reports are generated, before the ganache server is shut down. Useful if you need to clean resources up. More...
configureYulOptimizer Boolean false (Experimental) Setting to true should resolve "stack too deep" compiler errors in large projects using ABIEncoderV2
solcOptimizerDetails Object undefined (Experimental) Must be used in combination with configureYulOptimizer. Allows you configure solc's optimizer details. Useful if the default remedy for stack-too-deep errors doesn't work in your case (See FAQ below).

* Advanced use

API

Solidity-coverage's core methods and many utilities are available as an API.

const CoverageAPI = require('solidity-coverage/api');

Documentation available here.

Detecting solidity-coverage from another task

If you're writing another plugin or task, it can be helpful to detect whether coverage is running or not. The coverage plugin sets a boolean variable on the globally injected hardhat environment object for this purpose.

hre.__SOLIDITY_COVERAGE_RUNNING === true

FAQ

Common problems & questions:

Example reports

Contribution Guidelines

Contributions are welcome! If you're opening a PR that adds features or options please consider writing full unit tests for them. (We've built simple fixtures for almost everything and are happy to add some for your case if necessary).

Set up the development environment with:

$ git clone https://github.com/sc-forks/solidity-coverage.git
$ yarn

Contributors