Skip to content

Commit

Permalink
Add test coverage support for Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 10, 2024
1 parent 8fa299f commit bea2f9a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Add a `copy_to_uninit()` method to all `TypedArray`s. It takes `&mut [MaybeUninit<T>]` and returns `&mut [T]`.
[#4340](https://github.com/rustwasm/wasm-bindgen/pull/4340)

* Add test coverage support for Node.js.
[#4348](https://github.com/rustwasm/wasm-bindgen/pull/4348)

### Changed

* Optional parameters are now typed as `T | undefined | null` to reflect the actual JS behavior.
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fn main() -> anyhow::Result<()> {

match test_mode {
TestMode::Node { no_modules } => {
node::execute(module, &tmpdir, &args, &tests, !no_modules)?
node::execute(module, &tmpdir, &args, &tests, !no_modules, coverage)?
}
TestMode::Deno => deno::execute(module, &tmpdir, &args, &tests)?,
TestMode::Browser { .. }
Expand Down
15 changes: 14 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{Context, Error};
Expand Down Expand Up @@ -44,10 +44,12 @@ pub fn execute(
args: &[OsString],
tests: &[String],
module_format: bool,
coverage: PathBuf,
) -> Result<(), Error> {
let mut js_to_execute = format!(
r#"
{exit};
{fs};
{wasm};
{console_override}
Expand All @@ -62,6 +64,11 @@ pub fn execute(
cx.args(process.argv.slice(2));
const ok = await cx.run(tests.map(n => wasm.__wasm[n]));
const coverage = wasm.__wbgtest_cov_dump();
if (coverage !== undefined)
await fs.writeFile('{coverage}', coverage);
if (!ok)
exit(1);
}}
Expand All @@ -78,6 +85,12 @@ pub fn execute(
} else {
r"import { exit } from 'node:process'".to_string()
},
fs = if !module_format {
r"const fs = require('node:fs/promises')".to_string()
} else {
r"import fs from 'node:fs/promises'".to_string()
},
coverage = coverage.display(),
console_override = SHARED_SETUP,
);

Expand Down

0 comments on commit bea2f9a

Please sign in to comment.