Skip to content

Commit 41eefa7

Browse files
committed
wasm-bindgen-test: Capture more console logging methods' output
Fixes #1183
1 parent d5b6c52 commit 41eefa7

File tree

5 files changed

+115
-82
lines changed

5 files changed

+115
-82
lines changed

crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,38 @@
33
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
44
</head>
55
<body>
6-
<pre id='output'>Loading scripts...</pre>
7-
<pre id='console_log'></pre>
8-
<pre id='console_error'></pre>
6+
<pre id="output">Loading scripts...</pre>
7+
<pre id="console_debug"></pre>
8+
<pre id="console_log"></pre>
9+
<pre id="console_info"></pre>
10+
<pre id="console_warn"></pre>
11+
<pre id="console_error"></pre>
912
<script>
10-
const orig_console_log = function(...args) {
11-
const logs = document.getElementById('console_log');
12-
for (let msg of args) {
13-
logs.innerHTML += `${msg}\n`;
14-
}
15-
};
13+
const orig = id => (...args) => {
14+
const logs = document.getElementById(id);
15+
for (let msg of args) {
16+
logs.innerHTML += `${msg}\n`;
17+
}
18+
};
1619

17-
const orig_console_error = function(...args) {
18-
const logs = document.getElementById('console_error');
19-
for (let msg of args) {
20-
logs.innerHTML += `${msg}\n`;
21-
}
22-
};
20+
const wrap = method => {
21+
const og = orig(`console_${method}`);
22+
const on_method = `on_console_${method}`;
23+
console[method] = function (...args) {
24+
if (window[on_method]) {
25+
window[on_method](args);
26+
}
27+
og.apply(this, args);
28+
};
29+
};
2330

24-
console.log = function(...args) {
25-
if (window.on_console_log) {
26-
window.on_console_log(args);
27-
}
31+
wrap("debug");
32+
wrap("log");
33+
wrap("info");
34+
wrap("warn");
35+
wrap("error");
2836

29-
orig_console_log.apply(this, args);
30-
};
31-
32-
console.error = function(...args) {
33-
if (window.on_console_error) {
34-
window.on_console_error(args);
35-
}
36-
37-
orig_console_error.apply(this, args);
38-
};
39-
40-
window.__wbg_test_invoke = f => f();
37+
window.__wbg_test_invoke = f => f();
4138
</script>
4239
<script src='run.js' type=module></script>
4340
</body>

crates/cli/src/bin/wasm-bindgen-test-runner/index.html

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@
55
<body>
66
<pre id='output'>Loading scripts...</pre>
77
<script>
8-
const orig_console_log = console.log;
9-
const orig_console_error = console.error;
8+
const wrap = method => {
9+
const og = console[method];
10+
const on_method = `on_console_${method}`;
11+
console[method] = function (...args) {
12+
if (window[on_method]) {
13+
window[on_method](args);
14+
}
15+
og.apply(this, args);
16+
};
17+
};
1018

11-
console.log = function(...args) {
12-
if (window.on_console_log) {
13-
window.on_console_log(args);
14-
}
19+
wrap("debug");
20+
wrap("log");
21+
wrap("info");
22+
wrap("warn");
23+
wrap("error");
1524

16-
orig_console_log.apply(this, args);
17-
};
18-
19-
console.error = function(...args) {
20-
if (window.on_console_error) {
21-
window.on_console_error(args);
22-
}
23-
24-
orig_console_error.apply(this, args);
25-
};
26-
27-
window.__wbg_test_invoke = f => f();
25+
window.__wbg_test_invoke = f => f();
2826
</script>
2927
<script src='run.js' type=module></script>
3028
</body>

crates/cli/src/bin/wasm-bindgen-test-runner/node.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@ pub fn execute(
1616
r#"
1717
const {{ exit }} = require('process');
1818
19-
let on_console_log = null;
20-
let on_console_error = null;
19+
const handlers = {{}};
2120
22-
// override `console.log` and `console.error` before we import tests to
21+
const wrap = method => {{
22+
const og = console[method];
23+
const on_method = `on_console_${{method}}`;
24+
console[method] = function (...args) {{
25+
if (handlers[on_method]) {{
26+
handlers[on_method](args);
27+
}}
28+
og.apply(this, args);
29+
}};
30+
}};
31+
32+
// override `console.log` and `console.error` etc... before we import tests to
2333
// ensure they're bound correctly in wasm. This'll allow us to intercept
2434
// all these calls and capture the output of tests
25-
const prev_log = console.log;
26-
console.log = function(...args) {{
27-
if (on_console_log) {{
28-
on_console_log(args);
29-
}}
30-
prev_log.apply(null, args);
31-
}};
32-
const prev_error = console.error;
33-
console.error = function(...args) {{
34-
if (on_console_error) {{
35-
on_console_error(args);
36-
}}
37-
prev_error.apply(null, args);
38-
}};
35+
wrap("debug");
36+
wrap("log");
37+
wrap("info");
38+
wrap("warn");
39+
wrap("error");
3940
4041
global.__wbg_test_invoke = f => f();
4142
@@ -44,8 +45,11 @@ pub fn execute(
4445
const wasm = require("./{0}_bg");
4546
4647
cx = new support.Context();
47-
on_console_log = support.__wbgtest_console_log;
48-
on_console_error = support.__wbgtest_console_error;
48+
handlers.on_console_debug = support.__wbgtest_console_debug;
49+
handlers.on_console_log = support.__wbgtest_console_log;
50+
handlers.on_console_info = support.__wbgtest_console_info;
51+
handlers.on_console_warn = support.__wbgtest_console_warn;
52+
handlers.on_console_error = support.__wbgtest_console_error;
4953
5054
// Forward runtime arguments. These arguments are also arguments to the
5155
// `wasm-bindgen-test-runner` which forwards them to node which we

crates/cli/src/bin/wasm-bindgen-test-runner/server.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ pub fn spawn(
1717
) -> Result<Server<impl Fn(&Request) -> Response + Send + Sync>, Error> {
1818
let mut js_to_execute = format!(
1919
r#"
20-
import {{ Context, __wbgtest_console_log, __wbgtest_console_error }} from './{0}';
20+
import {{
21+
Context,
22+
__wbgtest_console_debug,
23+
__wbgtest_console_log,
24+
__wbgtest_console_info,
25+
__wbgtest_console_warn,
26+
__wbgtest_console_error
27+
}} from './{0}';
2128
import * as wasm from './{0}_bg';
2229
2330
// Now that we've gotten to the point where JS is executing, update our
@@ -31,7 +38,10 @@ pub fn spawn(
3138
await wasm.booted;
3239
3340
const cx = new Context();
41+
window.on_console_debug = __wbgtest_console_debug;
3442
window.on_console_log = __wbgtest_console_log;
43+
window.on_console_info = __wbgtest_console_info;
44+
window.on_console_warn = __wbgtest_console_warn;
3545
window.on_console_error = __wbgtest_console_error;
3646
3747
// Forward runtime arguments. These arguments are also arguments to the

crates/test/src/rt/mod.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ struct Test {
164164
/// Captured output of each test.
165165
#[derive(Default)]
166166
struct Output {
167+
debug: String,
167168
log: String,
169+
info: String,
170+
warn: String,
168171
error: String,
169172
}
170173

@@ -309,9 +312,25 @@ pub fn __wbgtest_console_log(args: &Array) {
309312
record(args, |output| &mut output.log)
310313
}
311314

312-
/// Handler for `console.error` invocations.
313-
///
314-
/// Works the same as `console_log` above.
315+
/// Handler for `console.debug` invocations. See above.
316+
#[wasm_bindgen]
317+
pub fn __wbgtest_console_debug(args: &Array) {
318+
record(args, |output| &mut output.debug)
319+
}
320+
321+
/// Handler for `console.info` invocations. See above.
322+
#[wasm_bindgen]
323+
pub fn __wbgtest_console_info(args: &Array) {
324+
record(args, |output| &mut output.info)
325+
}
326+
327+
/// Handler for `console.warn` invocations. See above.
328+
#[wasm_bindgen]
329+
pub fn __wbgtest_console_warn(args: &Array) {
330+
record(args, |output| &mut output.warn)
331+
}
332+
333+
/// Handler for `console.error` invocations. See above.
315334
#[wasm_bindgen]
316335
pub fn __wbgtest_console_error(args: &Array) {
317336
record(args, |output| &mut output.error)
@@ -477,19 +496,24 @@ impl State {
477496
));
478497
}
479498

499+
fn accumulate_console_output(&self, logs: &mut String, which: &str, output: &str) {
500+
if output.is_empty() {
501+
return;
502+
}
503+
logs.push_str(which);
504+
logs.push_str(" output:\n");
505+
logs.push_str(&tab(output));
506+
logs.push('\n');
507+
}
508+
480509
fn print_failure(&self, test: &Test, error: &JsValue) {
481510
let mut logs = String::new();
482511
let output = test.output.borrow();
483-
if output.log.len() > 0 {
484-
logs.push_str("log output:\n");
485-
logs.push_str(&tab(&output.log));
486-
logs.push_str("\n");
487-
}
488-
if output.error.len() > 0 {
489-
logs.push_str("error output:\n");
490-
logs.push_str(&tab(&output.error));
491-
logs.push_str("\n");
492-
}
512+
self.accumulate_console_output(&mut logs, "debug", &output.debug);
513+
self.accumulate_console_output(&mut logs, "log", &output.log);
514+
self.accumulate_console_output(&mut logs, "info", &output.info);
515+
self.accumulate_console_output(&mut logs, "warn", &output.warn);
516+
self.accumulate_console_output(&mut logs, "error", &output.error);
493517
logs.push_str("JS exception that was thrown:\n");
494518
let error_string = self.formatter.stringify_error(error);
495519
logs.push_str(&tab(&error_string));

0 commit comments

Comments
 (0)