diff --git a/CHANGELOG.md b/CHANGELOG.md index c1a7a44bd4b..72bc905f84f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ * Support importing memory and using `wasm_bindgen::module()` in Node.js. [#4349](https://github.com/rustwasm/wasm-bindgen/pull/4349) -* Add `--list`, `--ignored` and `--exact` to `wasm-bindgen-test-runner`, analogous to `cargo test`. +* Add `--list`, `--ignored`, `--exact` and `--nocapture` to `wasm-bindgen-test-runner`, analogous to `cargo test`. [#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356) ### Changed @@ -37,6 +37,9 @@ * Remove `WASM_BINDGEN_THREADS_MAX_MEMORY` and `WASM_BINDGEN_THREADS_STACK_SIZE`. The maximum memory size can be set via `-Clink-arg=--max-memory=`. The stack size of a thread can be set when initializing the thread via the `default` function. [#4363](https://github.com/rustwasm/wasm-bindgen/pull/4363) +* `console.*()` calls in tests are now always intercepted by default. To show them use `--nocapture`. When shown they are always printed in-place instead of after test results, analogous to `cargo test`. + [#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356) + ### Fixed - Fixed using [JavaScript keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords) as identifiers not being handled correctly. diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/deno.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/deno.rs index 58c824e0b31..019eb6f9123 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/deno.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/deno.rs @@ -13,6 +13,7 @@ pub fn execute(module: &str, tmpdir: &Path, cli: Cli, tests: &[String]) -> Resul let mut js_to_execute = format!( r#"import * as wasm from "./{module}.js"; + const nocapture = {nocapture}; {console_override} window.__wbg_test_invoke = f => f(); @@ -21,6 +22,7 @@ pub fn execute(module: &str, tmpdir: &Path, cli: Cli, tests: &[String]) -> Resul const tests = []; "#, + nocapture = cli.nocapture.clone(), console_override = SHARED_SETUP, args = cli.into_args(), ); diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/headless.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/headless.rs index 6064475312d..d39229312b2 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/headless.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/headless.rs @@ -210,14 +210,15 @@ pub fn run( println!("output div contained:\n{}", tab(&output)); } } - if !logs.is_empty() { - println!("console.log div contained:\n{}", tab(&logs)); - } - if !errors.is_empty() { - println!("console.log div contained:\n{}", tab(&errors)); - } if !output.contains("test result: ok") { + if !logs.is_empty() { + println!("console.log div contained:\n{}", tab(&logs)); + } + if !errors.is_empty() { + println!("console.log div contained:\n{}", tab(&errors)); + } + bail!("some tests failed") } diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html b/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html index 790245f20db..e5af4a0eb61 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html @@ -18,10 +18,14 @@ } }; + // {NOCAPTURE} const wrap = method => { const og = orig(`console_${method}`); const on_method = `on_console_${method}`; console[method] = function (...args) { + if (nocapture) { + orig("output").apply(this, args); + } if (window[on_method]) { window[on_method](args); } 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 0c7ef99dc28..a16e860fb12 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs @@ -49,6 +49,11 @@ struct Cli { skip: Vec, #[arg(long, help = "List all tests and benchmarks")] list: bool, + #[arg( + long, + help = "don't capture `console.*()` of each task, allow printing directly" + )] + nocapture: bool, #[arg( long, value_enum, 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 c9bb0a7d4d4..68249ad7da9 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs @@ -15,13 +15,17 @@ const wrap = method => { const og = console[method]; const on_method = `on_console_${method}`; console[method] = function (...args) { - og.apply(this, args); + if (nocapture) { + og.apply(this, args); + } if (handlers[on_method]) { handlers[on_method](args); } }; }; +// save original `console.log` +global.__wbgtest_og_console_log = console.log; // override `console.log` and `console.error` etc... before we import tests to // ensure they're bound correctly in wasm. This'll allow us to intercept // all these calls and capture the output of tests @@ -53,6 +57,7 @@ pub fn execute( {fs}; {wasm}; + const nocapture = {nocapture}; {console_override} global.__wbg_test_invoke = f => f(); @@ -88,6 +93,7 @@ pub fn execute( r"import fs from 'node:fs/promises'".to_string() }, coverage = coverage.display(), + nocapture = cli.nocapture.clone(), console_override = SHARED_SETUP, args = cli.into_args(), ); diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs index cae510afc43..88999940e52 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs @@ -70,6 +70,7 @@ pub(crate) fn spawn( ) }; + let nocapture = cli.nocapture; let args = cli.into_args(); if test_mode.is_worker() { @@ -102,9 +103,13 @@ pub(crate) fn spawn( worker_script.push_str(&format!( r#" + const nocapture = {nocapture}; const wrap = method => {{ const on_method = `on_console_${{method}}`; self.console[method] = function (...args) {{ + if (nocapture) {{ + self.__wbg_test_output_writeln(args); + }} if (self[on_method]) {{ self[on_method](args); }} @@ -297,6 +302,7 @@ pub(crate) fn spawn( } else { include_str!("index.html") }; + let s = s.replace("// {NOCAPTURE}", &format!("const nocapture = {nocapture};")); let s = if !test_mode.is_worker() && test_mode.no_modules() { s.replace( "", diff --git a/crates/test/src/rt/node.rs b/crates/test/src/rt/node.rs index a505564f1e8..40e5276b3dd 100644 --- a/crates/test/src/rt/node.rs +++ b/crates/test/src/rt/node.rs @@ -19,6 +19,8 @@ extern "C" { type NodeError; #[wasm_bindgen(method, getter, js_class = "Error", structural)] fn stack(this: &NodeError) -> String; + #[wasm_bindgen(js_name = __wbgtest_og_console_log)] + fn og_console_log(s: &str); } impl Node { @@ -30,7 +32,7 @@ impl Node { impl super::Formatter for Node { fn writeln(&self, line: &str) { - super::js_console_log(line); + og_console_log(line); } fn log_test(&self, name: &str, result: &TestResult) {