Skip to content

Commit b7f8bfc

Browse files
Document coverage in the guide
1 parent e8f74a2 commit b7f8bfc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

guide/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
- [Writing Asynchronous Tests](./wasm-bindgen-test/asynchronous-tests.md)
111111
- [Testing in Headless Browsers](./wasm-bindgen-test/browsers.md)
112112
- [Continuous Integration](./wasm-bindgen-test/continuous-integration.md)
113+
- [Coverage (Experimental)](./wasm-bindgen-test/coverage.md)
113114

114115
- [Contributing to `wasm-bindgen`](./contributing/index.md)
115116
- [Testing](./contributing/testing.md)
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)