|
| 1 | +# Generating Coverage Data |
| 2 | + |
| 3 | +You can ask the runner to generate coverage data from functions marked as `#[wasm_bindgen_test]` in the `.profraw` format. |
| 4 | + |
| 5 | +<div class="warning"> |
| 6 | + Coverage is still in an experimental state and may be unreliable and could experience |
| 7 | + breaking changes at any time. |
| 8 | +</div> |
| 9 | + |
| 10 | +## Enabling the feature |
| 11 | + |
| 12 | +To enable this feature, you need to enable the `"unstable-coverage"` feature in your `wasm-bindgen-test` dependency. |
| 13 | + |
| 14 | +## Generating the data |
| 15 | + |
| 16 | +### `RUSTFLAGS` that need to be present |
| 17 | + |
| 18 | +Make sure you are using `RUSTFLAGS=-Cinstrument-coverage -Zno-profiler-runtime`. |
| 19 | + |
| 20 | +Due to the current limitation of `llvm-cov`, you can't get debug information out of a `.wasm`. |
| 21 | +Instead, [we can grab them from the LLVM IR][wasmcov], so consider adding `--emit=llvm-ir` as well. |
| 22 | + |
| 23 | +[wasmcov]: https://github.com/hknio/code-coverage-for-webassembly |
| 24 | + |
| 25 | +### Arguments to the test runner |
| 26 | + |
| 27 | +If you are using `wasm-pack` to run the tests, refer to the `wasm-pack` documentation. |
| 28 | + |
| 29 | +Otherwise, you can use the following environment variables when [executing the test runner][1] to control the coverage output: |
| 30 | + |
| 31 | +[1]: usage.html#appendix-using-wasm-bindgen-test-without-wasm-pack |
| 32 | + |
| 33 | +- `WASM_BINDGEN_UNSTABLE_TEST_COVERAGE` to generate a single `.profraw` in your current working directory. |
| 34 | +- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT` to control the file name of the profraw or the directory in which it is placed |
| 35 | +- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_PREFIX` to add a custom prefix to the profraw files. This can be useful if you're running the tests automatically in succession. |
| 36 | + |
| 37 | +### Example |
| 38 | + |
| 39 | +```sh |
| 40 | +RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir" wasm-pack test --coverage --profraw-out cov_data/ |
| 41 | +# Generate the debug info on the host |
| 42 | +clang target/wasm32-unknown-unknown/debug/deps/{your-dot-wasm-without-extension}.ll -Wno-override-module -c -o wasm.o |
| 43 | +llvm-profdata merge --sparse cov_data/*.profraw -o cov_data/coverage.profdata |
| 44 | +llvm-cov --instr-profile=cov_data/coverage.profdata wasm.o --format=html --output-dir=coverage/ --sources . |
| 45 | +``` |
0 commit comments