wasm_bindgen_test/rt/
node.rs

1//! Support for printing status information of a test suite in node.js
2//!
3//! This currently uses the same output as `libtest`, only reimplemented here
4//! for node itself.
5
6use alloc::string::String;
7use wasm_bindgen::prelude::*;
8
9/// Implementation of the `Formatter` trait for node.js
10pub struct Node {}
11
12#[wasm_bindgen]
13extern "C" {
14    // Not using `js_sys::Error` because node's errors specifically have a
15    // `stack` attribute.
16    type NodeError;
17    #[wasm_bindgen(method, getter, js_class = "Error", structural)]
18    fn stack(this: &NodeError) -> String;
19    #[wasm_bindgen(js_name = __wbgtest_og_console_log)]
20    fn og_console_log(s: &str);
21}
22
23impl Node {
24    /// Attempts to create a new formatter for node.js
25    pub fn new() -> Node {
26        Node {}
27    }
28}
29
30impl super::Formatter for Node {
31    fn writeln(&self, line: &str) {
32        og_console_log(line);
33    }
34
35    fn stringify_error(&self, err: &JsValue) -> String {
36        // TODO: should do a checked cast to `NodeError`
37        NodeError::from(err.clone()).stack()
38    }
39}