Skip to content

Commit

Permalink
Add test coverage support for Node.js (rustwasm#4348)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 19, 2024
1 parent 99bff3e commit e979061
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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 e979061

Please sign in to comment.