|
| 1 | +[][18] |
| 2 | + |
| 3 | +[][20] |
| 4 | +[][21] |
| 5 | +[][26] |
| 6 | + |
| 7 | +# solidity-coverage |
| 8 | + |
| 9 | +Solidity code coverage plugin for [Hardhat](http://hardhat.org). |
| 10 | + |
| 11 | +## What |
| 12 | + |
| 13 | +![coverage example][22] |
| 14 | + |
| 15 | ++ For more details about how it works and potential limitations, see [the accompanying article][16]. |
| 16 | ++ `solidity-coverage` is also [JoinColony/solcover][17] |
| 17 | + |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +$ npm install --save-dev solidity-coverage |
| 23 | +``` |
| 24 | + |
| 25 | +And add the following to your `.config.js`: |
| 26 | + |
| 27 | +```js |
| 28 | +require("solidity-coverage"); |
| 29 | +``` |
| 30 | + |
| 31 | +Or, if you are using TypeScript, add this to your hardhat.config.ts: |
| 32 | + |
| 33 | +```ts |
| 34 | +import "solidity-coverage" |
| 35 | +``` |
| 36 | + |
| 37 | +## Tasks |
| 38 | + |
| 39 | +This plugin implements a `coverage` task |
| 40 | + |
| 41 | +```bash |
| 42 | +npx hardhat coverage [options] |
| 43 | +``` |
| 44 | + |
| 45 | +| Option <img width=200/> | Example <img width=750/>| Description <img width=1000/> | |
| 46 | +|--------------|------------------------------------|--------------------------------| |
| 47 | +| testfiles | `--testfiles "test/registry/*.ts"` | Test file(s) to run. (Globs must be enclosed by quotes.)| |
| 48 | +| solcoverjs | `--solcoverjs ./../.solcover.js` | Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed) | |
| 49 | +| network | `--network development` | Run with a ganache client over http using network settings defined in `hardhat.config.js`. (Hardhat is the default network) | |
| 50 | + |
| 51 | + |
| 52 | +## Configuration |
| 53 | + |
| 54 | +Options can be specified in a `.solcover.js` config file located in the root directory of your project. |
| 55 | + |
| 56 | +**Config Example:** |
| 57 | +```javascript |
| 58 | +module.exports = { |
| 59 | + skipFiles: ['Routers/EtherRouter.sol'] |
| 60 | +}; |
| 61 | +``` |
| 62 | + |
| 63 | +| Option <img width=200/>| Type <img width=200/> | Default <img width=1300/> | Description <img width=800/> | |
| 64 | +| ------ | ---- | ------- | ----------- | |
| 65 | +| silent | *Boolean* | false | Suppress logging output | |
| 66 | +| client | *Object* | undefined | *Ganache only*: Useful if you need a specific ganache version. An example value is: `require('ganache-cli')` | |
| 67 | +| providerOptions | *Object* | `{ }` | *Ganache only*: [ganache-core options][1] | |
| 68 | +| skipFiles | *Array* | `['Migrations.sol']` | Array of contracts or folders (with paths expressed relative to the `contracts` directory) that should be skipped when doing instrumentation. | |
| 69 | +| istanbulFolder | *String* | `./coverage` | Folder location for Istanbul coverage reports. | |
| 70 | +| istanbulReporter | *Array* | `['html', 'lcov', 'text', 'json']` | [Istanbul coverage reporters][2] | |
| 71 | +| mocha | *Object* | `{ }` | [Mocha options][3] to merge into existing mocha config. `grep` and `invert` are useful for skipping certain tests under coverage using tags in the test descriptions.| |
| 72 | +| onServerReady[<sup>*</sup>][14] | *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...][23] | |
| 73 | +| onCompileComplete[<sup>*</sup>][14] | *Function* | | Hook run *after* compilation completes, *before* tests are run. Useful if you have secondary compilation steps or need to modify built artifacts. [More...][23]| |
| 74 | +| onTestsComplete[<sup>*</sup>][14] | *Function* | | Hook run *after* the tests complete, *before* Istanbul reports are generated. [More...][23]| |
| 75 | +| onIstanbulComplete[<sup>*</sup>][14] | *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...][23]| |
| 76 | + |
| 77 | +[<sup>*</sup> Advanced use][14] |
| 78 | + |
| 79 | +## Usage |
| 80 | + |
| 81 | ++ Coverage runs tests a little more slowly. |
| 82 | ++ Coverage uses the Hardhat network by default. |
| 83 | ++ Coverage [distorts gas consumption][13]. Tests that check exact gas consumption should be [skipped][24]. |
| 84 | ++ :warning: Contracts are compiled **without optimization**. Please report unexpected compilation faults to [issue 417][25] |
| 85 | + |
| 86 | +## Using with ganache |
| 87 | + |
| 88 | +Begining with `v0.7.12`, this plugin runs directly on the Hardhat network by default (for speed). |
| 89 | + |
| 90 | +If you want to use a ganache based http network, you can specify it by name using the `--network` cli option. The plugin will then launch its own coverage enabled ganache instance which can be configured in `.solcover.js` via the `providerOptions` key. |
| 91 | + |
| 92 | +## Documentation |
| 93 | + |
| 94 | +More documentation, including FAQ and information about solidity-coverage's API [is available here][28]. |
| 95 | + |
| 96 | + |
| 97 | +[1]: https://github.com/trufflesuite/ganache-core#options |
| 98 | +[2]: https://istanbul.js.org/docs/advanced/alternative-reporters/ |
| 99 | +[3]: https://mochajs.org/api/mocha |
| 100 | +[4]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/faq.md#running-out-of-gas |
| 101 | +[5]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/faq.md#running-out-of-memory |
| 102 | +[6]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/faq.md#running-out-of-time |
| 103 | +[7]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/faq.md#continuous-integration |
| 104 | +[8]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/faq.md#notes-on-branch-coverage |
| 105 | +[9]: https://sc-forks.github.io/metacoin/ |
| 106 | +[10]: https://coveralls.io/github/OpenZeppelin/openzeppelin-solidity?branch=master |
| 107 | +[11]: https://github.com/sc-forks/solidity-coverage/tree/master/test/units |
| 108 | +[12]: https://github.com/sc-forks/solidity-coverage/issues |
| 109 | +[13]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/faq.md#notes-on-gas-distortion |
| 110 | +[14]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/advanced.md |
| 111 | +[15]: #config-options |
| 112 | +[16]: https://blog.colony.io/code-coverage-for-solidity-eecfa88668c2 |
| 113 | +[17]: https://github.com/JoinColony/solcover |
| 114 | +[18]: https://gitter.im/sc-forks/solidity-coverage?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge |
| 115 | +[19]: https://badge.fury.io/js/solidity-coverage |
| 116 | +[20]: https://circleci.com/gh/sc-forks/solidity-coverage |
| 117 | +[21]: https://codecov.io/gh/sc-forks/solidity-coverage |
| 118 | +[22]: https://cdn-images-1.medium.com/max/800/1*uum8t-31bUaa6dTRVVhj6w.png |
| 119 | +[23]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/advanced.md#workflow-hooks |
| 120 | +[24]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/advanced.md#skipping-tests |
| 121 | +[25]: https://github.com/sc-forks/solidity-coverage/issues/417 |
| 122 | +[26]: https://hardhat.org/ |
| 123 | +[27]: https://www.trufflesuite.com/docs |
| 124 | +[28]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/api.md |
| 125 | +[29]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/upgrade.md#upgrading-from-06x-to-070 |
| 126 | +[30]: https://github.com/sc-forks/solidity-coverage/tree/0.6.x-final#solidity-coverage |
| 127 | +[31]: https://github.com/sc-forks/solidity-coverage/releases/tag/v0.7.0 |
| 128 | +[32]: https://github.com/sc-forks/buidler-e2e/tree/coverage |
| 129 | +[33]: https://github.com/sc-forks/moloch |
| 130 | +[34]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/advanced.md#reducing-the-instrumentation-footprint |
| 131 | + |
0 commit comments