mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-24 18:43:33 +03:00
wasm-bindgen-test: Capture more console logging methods' output
Fixes #1183
This commit is contained in:
parent
d5b6c5270b
commit
41eefa7425
@ -3,41 +3,38 @@
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
|
||||
</head>
|
||||
<body>
|
||||
<pre id='output'>Loading scripts...</pre>
|
||||
<pre id='console_log'></pre>
|
||||
<pre id='console_error'></pre>
|
||||
<pre id="output">Loading scripts...</pre>
|
||||
<pre id="console_debug"></pre>
|
||||
<pre id="console_log"></pre>
|
||||
<pre id="console_info"></pre>
|
||||
<pre id="console_warn"></pre>
|
||||
<pre id="console_error"></pre>
|
||||
<script>
|
||||
const orig_console_log = function(...args) {
|
||||
const logs = document.getElementById('console_log');
|
||||
for (let msg of args) {
|
||||
logs.innerHTML += `${msg}\n`;
|
||||
}
|
||||
};
|
||||
const orig = id => (...args) => {
|
||||
const logs = document.getElementById(id);
|
||||
for (let msg of args) {
|
||||
logs.innerHTML += `${msg}\n`;
|
||||
}
|
||||
};
|
||||
|
||||
const orig_console_error = function(...args) {
|
||||
const logs = document.getElementById('console_error');
|
||||
for (let msg of args) {
|
||||
logs.innerHTML += `${msg}\n`;
|
||||
}
|
||||
};
|
||||
const wrap = method => {
|
||||
const og = orig(`console_${method}`);
|
||||
const on_method = `on_console_${method}`;
|
||||
console[method] = function (...args) {
|
||||
if (window[on_method]) {
|
||||
window[on_method](args);
|
||||
}
|
||||
og.apply(this, args);
|
||||
};
|
||||
};
|
||||
|
||||
console.log = function(...args) {
|
||||
if (window.on_console_log) {
|
||||
window.on_console_log(args);
|
||||
}
|
||||
wrap("debug");
|
||||
wrap("log");
|
||||
wrap("info");
|
||||
wrap("warn");
|
||||
wrap("error");
|
||||
|
||||
orig_console_log.apply(this, args);
|
||||
};
|
||||
|
||||
console.error = function(...args) {
|
||||
if (window.on_console_error) {
|
||||
window.on_console_error(args);
|
||||
}
|
||||
|
||||
orig_console_error.apply(this, args);
|
||||
};
|
||||
|
||||
window.__wbg_test_invoke = f => f();
|
||||
window.__wbg_test_invoke = f => f();
|
||||
</script>
|
||||
<script src='run.js' type=module></script>
|
||||
</body>
|
||||
|
@ -5,26 +5,24 @@
|
||||
<body>
|
||||
<pre id='output'>Loading scripts...</pre>
|
||||
<script>
|
||||
const orig_console_log = console.log;
|
||||
const orig_console_error = console.error;
|
||||
const wrap = method => {
|
||||
const og = console[method];
|
||||
const on_method = `on_console_${method}`;
|
||||
console[method] = function (...args) {
|
||||
if (window[on_method]) {
|
||||
window[on_method](args);
|
||||
}
|
||||
og.apply(this, args);
|
||||
};
|
||||
};
|
||||
|
||||
console.log = function(...args) {
|
||||
if (window.on_console_log) {
|
||||
window.on_console_log(args);
|
||||
}
|
||||
wrap("debug");
|
||||
wrap("log");
|
||||
wrap("info");
|
||||
wrap("warn");
|
||||
wrap("error");
|
||||
|
||||
orig_console_log.apply(this, args);
|
||||
};
|
||||
|
||||
console.error = function(...args) {
|
||||
if (window.on_console_error) {
|
||||
window.on_console_error(args);
|
||||
}
|
||||
|
||||
orig_console_error.apply(this, args);
|
||||
};
|
||||
|
||||
window.__wbg_test_invoke = f => f();
|
||||
window.__wbg_test_invoke = f => f();
|
||||
</script>
|
||||
<script src='run.js' type=module></script>
|
||||
</body>
|
||||
|
@ -16,26 +16,27 @@ pub fn execute(
|
||||
r#"
|
||||
const {{ exit }} = require('process');
|
||||
|
||||
let on_console_log = null;
|
||||
let on_console_error = null;
|
||||
const handlers = {{}};
|
||||
|
||||
// override `console.log` and `console.error` before we import tests to
|
||||
const wrap = method => {{
|
||||
const og = console[method];
|
||||
const on_method = `on_console_${{method}}`;
|
||||
console[method] = function (...args) {{
|
||||
if (handlers[on_method]) {{
|
||||
handlers[on_method](args);
|
||||
}}
|
||||
og.apply(this, args);
|
||||
}};
|
||||
}};
|
||||
|
||||
// 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
|
||||
const prev_log = console.log;
|
||||
console.log = function(...args) {{
|
||||
if (on_console_log) {{
|
||||
on_console_log(args);
|
||||
}}
|
||||
prev_log.apply(null, args);
|
||||
}};
|
||||
const prev_error = console.error;
|
||||
console.error = function(...args) {{
|
||||
if (on_console_error) {{
|
||||
on_console_error(args);
|
||||
}}
|
||||
prev_error.apply(null, args);
|
||||
}};
|
||||
wrap("debug");
|
||||
wrap("log");
|
||||
wrap("info");
|
||||
wrap("warn");
|
||||
wrap("error");
|
||||
|
||||
global.__wbg_test_invoke = f => f();
|
||||
|
||||
@ -44,8 +45,11 @@ pub fn execute(
|
||||
const wasm = require("./{0}_bg");
|
||||
|
||||
cx = new support.Context();
|
||||
on_console_log = support.__wbgtest_console_log;
|
||||
on_console_error = support.__wbgtest_console_error;
|
||||
handlers.on_console_debug = support.__wbgtest_console_debug;
|
||||
handlers.on_console_log = support.__wbgtest_console_log;
|
||||
handlers.on_console_info = support.__wbgtest_console_info;
|
||||
handlers.on_console_warn = support.__wbgtest_console_warn;
|
||||
handlers.on_console_error = support.__wbgtest_console_error;
|
||||
|
||||
// Forward runtime arguments. These arguments are also arguments to the
|
||||
// `wasm-bindgen-test-runner` which forwards them to node which we
|
||||
|
@ -17,7 +17,14 @@ pub fn spawn(
|
||||
) -> Result<Server<impl Fn(&Request) -> Response + Send + Sync>, Error> {
|
||||
let mut js_to_execute = format!(
|
||||
r#"
|
||||
import {{ Context, __wbgtest_console_log, __wbgtest_console_error }} from './{0}';
|
||||
import {{
|
||||
Context,
|
||||
__wbgtest_console_debug,
|
||||
__wbgtest_console_log,
|
||||
__wbgtest_console_info,
|
||||
__wbgtest_console_warn,
|
||||
__wbgtest_console_error
|
||||
}} from './{0}';
|
||||
import * as wasm from './{0}_bg';
|
||||
|
||||
// Now that we've gotten to the point where JS is executing, update our
|
||||
@ -31,7 +38,10 @@ pub fn spawn(
|
||||
await wasm.booted;
|
||||
|
||||
const cx = new Context();
|
||||
window.on_console_debug = __wbgtest_console_debug;
|
||||
window.on_console_log = __wbgtest_console_log;
|
||||
window.on_console_info = __wbgtest_console_info;
|
||||
window.on_console_warn = __wbgtest_console_warn;
|
||||
window.on_console_error = __wbgtest_console_error;
|
||||
|
||||
// Forward runtime arguments. These arguments are also arguments to the
|
||||
|
@ -164,7 +164,10 @@ struct Test {
|
||||
/// Captured output of each test.
|
||||
#[derive(Default)]
|
||||
struct Output {
|
||||
debug: String,
|
||||
log: String,
|
||||
info: String,
|
||||
warn: String,
|
||||
error: String,
|
||||
}
|
||||
|
||||
@ -309,9 +312,25 @@ pub fn __wbgtest_console_log(args: &Array) {
|
||||
record(args, |output| &mut output.log)
|
||||
}
|
||||
|
||||
/// Handler for `console.error` invocations.
|
||||
///
|
||||
/// Works the same as `console_log` above.
|
||||
/// Handler for `console.debug` invocations. See above.
|
||||
#[wasm_bindgen]
|
||||
pub fn __wbgtest_console_debug(args: &Array) {
|
||||
record(args, |output| &mut output.debug)
|
||||
}
|
||||
|
||||
/// Handler for `console.info` invocations. See above.
|
||||
#[wasm_bindgen]
|
||||
pub fn __wbgtest_console_info(args: &Array) {
|
||||
record(args, |output| &mut output.info)
|
||||
}
|
||||
|
||||
/// Handler for `console.warn` invocations. See above.
|
||||
#[wasm_bindgen]
|
||||
pub fn __wbgtest_console_warn(args: &Array) {
|
||||
record(args, |output| &mut output.warn)
|
||||
}
|
||||
|
||||
/// Handler for `console.error` invocations. See above.
|
||||
#[wasm_bindgen]
|
||||
pub fn __wbgtest_console_error(args: &Array) {
|
||||
record(args, |output| &mut output.error)
|
||||
@ -477,19 +496,24 @@ impl State {
|
||||
));
|
||||
}
|
||||
|
||||
fn accumulate_console_output(&self, logs: &mut String, which: &str, output: &str) {
|
||||
if output.is_empty() {
|
||||
return;
|
||||
}
|
||||
logs.push_str(which);
|
||||
logs.push_str(" output:\n");
|
||||
logs.push_str(&tab(output));
|
||||
logs.push('\n');
|
||||
}
|
||||
|
||||
fn print_failure(&self, test: &Test, error: &JsValue) {
|
||||
let mut logs = String::new();
|
||||
let output = test.output.borrow();
|
||||
if output.log.len() > 0 {
|
||||
logs.push_str("log output:\n");
|
||||
logs.push_str(&tab(&output.log));
|
||||
logs.push_str("\n");
|
||||
}
|
||||
if output.error.len() > 0 {
|
||||
logs.push_str("error output:\n");
|
||||
logs.push_str(&tab(&output.error));
|
||||
logs.push_str("\n");
|
||||
}
|
||||
self.accumulate_console_output(&mut logs, "debug", &output.debug);
|
||||
self.accumulate_console_output(&mut logs, "log", &output.log);
|
||||
self.accumulate_console_output(&mut logs, "info", &output.info);
|
||||
self.accumulate_console_output(&mut logs, "warn", &output.warn);
|
||||
self.accumulate_console_output(&mut logs, "error", &output.error);
|
||||
logs.push_str("JS exception that was thrown:\n");
|
||||
let error_string = self.formatter.stringify_error(error);
|
||||
logs.push_str(&tab(&error_string));
|
||||
|
Loading…
Reference in New Issue
Block a user