cgewecke
b1ee826ae5
|
10 months ago | |
---|---|---|
.circleci | 10 months ago | |
.github | 1 year ago | |
docs | 10 months ago | |
lib | 10 months ago | |
plugins | 10 months ago | |
scripts | 10 months ago | |
test | 10 months ago | |
.gitignore | 5 years ago | |
.npmignore | 5 years ago | |
CHANGELOG.md | 10 months ago | |
FUNDING.json | 10 months ago | |
HARDHAT_README.md | 1 year ago | |
LICENSE | 8 years ago | |
README.md | 10 months ago | |
api.js | 5 years ago | |
package.json | 10 months ago | |
utils.js | 5 years ago | |
yarn.lock | 10 months ago |
README.md
solidity-coverage
Code coverage for Solidity testing
- For more details about what this is, how it works and potential limitations, see the accompanying article.
solidity-coverage
is Solcover
Requirements
- Hardhat >= 2.11.0
Install
$ yarn add solidity-coverage --dev
Require the plugin in hardhat.config.js
(Hardhat docs)
require('solidity-coverage')
Or, if you are using TypeScript, add this to your hardhat.config.ts:
import 'solidity-coverage'
Resources:
Run
npx hardhat coverage [command-options]
Command Options
Option | Example | Description |
---|---|---|
testfiles | --testfiles "test/registry/*.ts" |
Test file(s) to run. (Globs must be enclosed by quotes and use globby matching patterns) |
sources | --sources myFolder or --sources myFile.sol |
Path to single folder or file to target for coverage. Path is relative to Hardhat's paths.sources (usually contracts/ ) |
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 Hardhat 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... |
matrix | --matrix |
Generate a JSON object that maps which mocha tests hit which lines of code. (Useful as an input for some fuzzing, mutation testing and fault-localization algorithms.) More... |
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 |
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... |
measureModifierCoverage | boolean | true |
Computes each modifier invocation as a code branch. More... |
modifierWhitelist | String[] | [] |
List of modifier names (ex: "onlyOwner") to exclude from branch measurement. (Useful for modifiers which prepare something instead of acting as a gate.)) |
matrixOutputPath | String | ./testMatrix.json |
Relative path to write test matrix JSON object to. More... |
mochaJsonOutputPath | String | ./mochaOutput.json |
Relative path to write mocha JSON reporter object to. More... |
abiOutputPath | String | ./humanReadableAbis.json |
Relative path to write diff-able ABI data to |
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). |
client | Object | require("ganache-core") |
Ganache only: useful if you need a specific ganache version |
providerOptions | Object | { } |
Ganache only: ganache-core options |
Viewing the reports:
- You can find the Istanbul reports written to the
./coverage/
folder generated in your root directory.
API
Solidity-coverage's core methods and many utilities are available as an API.
const CoverageAPI = require('solidity-coverage/api');
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:
- Running in CI
- Running out of gas
- Running out of time
- Running out of stack (Stack too deep)
- Running out of memory
- Running in parallel (in CI)
- Running coverage threshold checks
Example reports
- metacoin (Istanbul HTML)
- openzeppelin-solidity(Coveralls)
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
- @area
- @cgewecke
- @adriamb
- @cag
- @maurelian
- @rudolfix
- @phiferd
- @e11io
- @elenadimitrova
- @ukstv
- @vdrg
- @andresliva
- @DimitarSD
- @sohkai
- @bingen
- @pinkiebell
- @obernardovieira
- @angus-hamill
- @kandrianov
- @yxliang01
- @maxsam4
- @justinjmoses
- @JasoonS
- @feuGeneA
- @Dylan-Kerler
- @paulrberg
- @adjisb
- @Shelvak
- @rynobey
- @ZumZoom
- @fvictorio
- @ChristopherDedominici
- @joaoh9
- @ilovehackathons