Skip to content

Commit 3c07e86

Browse files
authored
Add test coverage support for Node.js (#4348)
1 parent 8fa299f commit 3c07e86

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* Add a `copy_to_uninit()` method to all `TypedArray`s. It takes `&mut [MaybeUninit<T>]` and returns `&mut [T]`.
99
[#4340](https://github.com/rustwasm/wasm-bindgen/pull/4340)
1010

11+
* Add test coverage support for Node.js.
12+
[#4348](https://github.com/rustwasm/wasm-bindgen/pull/4348)
13+
1114
### Changed
1215

1316
* Optional parameters are now typed as `T | undefined | null` to reflect the actual JS behavior.

crates/cli/src/bin/wasm-bindgen-test-runner/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn main() -> anyhow::Result<()> {
271271

272272
match test_mode {
273273
TestMode::Node { no_modules } => {
274-
node::execute(module, &tmpdir, &args, &tests, !no_modules)?
274+
node::execute(module, &tmpdir, &args, &tests, !no_modules, coverage)?
275275
}
276276
TestMode::Deno => deno::execute(module, &tmpdir, &args, &tests)?,
277277
TestMode::Browser { .. }

crates/cli/src/bin/wasm-bindgen-test-runner/node.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env;
22
use std::ffi::OsString;
33
use std::fs;
4-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
55
use std::process::Command;
66

77
use anyhow::{Context, Error};
@@ -44,10 +44,12 @@ pub fn execute(
4444
args: &[OsString],
4545
tests: &[String],
4646
module_format: bool,
47+
coverage: PathBuf,
4748
) -> Result<(), Error> {
4849
let mut js_to_execute = format!(
4950
r#"
5051
{exit};
52+
{fs};
5153
{wasm};
5254
5355
{console_override}
@@ -62,6 +64,11 @@ pub fn execute(
6264
cx.args(process.argv.slice(2));
6365
6466
const ok = await cx.run(tests.map(n => wasm.__wasm[n]));
67+
68+
const coverage = wasm.__wbgtest_cov_dump();
69+
if (coverage !== undefined)
70+
await fs.writeFile('{coverage}', coverage);
71+
6572
if (!ok)
6673
exit(1);
6774
}}
@@ -78,6 +85,12 @@ pub fn execute(
7885
} else {
7986
r"import { exit } from 'node:process'".to_string()
8087
},
88+
fs = if !module_format {
89+
r"const fs = require('node:fs/promises')".to_string()
90+
} else {
91+
r"import fs from 'node:fs/promises'".to_string()
92+
},
93+
coverage = coverage.display(),
8194
console_override = SHARED_SETUP,
8295
);
8396

0 commit comments

Comments
 (0)