Skip to content

Commit

Permalink
Add --nocapture
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 17, 2024
1 parent f3093e2 commit 31d6577
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 9 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=<size>`. 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.
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/src/bin/wasm-bindgen-test-runner/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(),
);
Expand Down
13 changes: 7 additions & 6 deletions crates/cli/src/bin/wasm-bindgen-test-runner/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ struct Cli {
skip: Vec<String>,
#[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,
Expand Down
8 changes: 7 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +57,7 @@ pub fn execute(
{fs};
{wasm};
const nocapture = {nocapture};
{console_override}
global.__wbg_test_invoke = f => f();
Expand Down Expand Up @@ -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(),
);
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/bin/wasm-bindgen-test-runner/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub(crate) fn spawn(
)
};

let nocapture = cli.nocapture;
let args = cli.into_args();

if test_mode.is_worker() {
Expand Down Expand Up @@ -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);
}}
Expand Down Expand Up @@ -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(
"<!-- {IMPORT_SCRIPTS} -->",
Expand Down
4 changes: 3 additions & 1 deletion crates/test/src/rt/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 31d6577

Please sign in to comment.