From bea2f9ae6fcc2209b4365bafea5b3c60762286ea Mon Sep 17 00:00:00 2001 From: daxpedda Date: Tue, 10 Dec 2024 09:50:45 +0100 Subject: [PATCH] Add test coverage support for Node.js --- CHANGELOG.md | 3 +++ .../cli/src/bin/wasm-bindgen-test-runner/main.rs | 2 +- .../cli/src/bin/wasm-bindgen-test-runner/node.rs | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95b8f96cbfa..b0cc02f21a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ * Add a `copy_to_uninit()` method to all `TypedArray`s. It takes `&mut [MaybeUninit]` 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. diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs index 76259378054..b1ebea28c1e 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs @@ -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 { .. } diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs index ba131cf5465..3ac475fcb3e 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs @@ -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}; @@ -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} @@ -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); }} @@ -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, );