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::format;
7use alloc::string::String;
8use wasm_bindgen::prelude::*;
9
10use super::TestResult;
11
12/// Implementation of the `Formatter` trait for node.js
13pub struct Node {}
14
15#[wasm_bindgen]
16extern "C" {
17    // Not using `js_sys::Error` because node's errors specifically have a
18    // `stack` attribute.
19    type NodeError;
20    #[wasm_bindgen(method, getter, js_class = "Error", structural)]
21    fn stack(this: &NodeError) -> String;
22    #[wasm_bindgen(js_name = __wbgtest_og_console_log)]
23    fn og_console_log(s: &str);
24}
25
26impl Node {
27    /// Attempts to create a new formatter for node.js
28    pub fn new() -> Node {
29        Node {}
30    }
31}
32
33impl super::Formatter for Node {
34    fn writeln(&self, line: &str) {
35        og_console_log(line);
36    }
37
38    fn log_test(&self, name: &str, result: &TestResult) {
39        self.writeln(&format!("test {} ... {}", name, result));
40    }
41
42    fn stringify_error(&self, err: &JsValue) -> String {
43        // TODO: should do a checked cast to `NodeError`
44        NodeError::from(err.clone()).stack()
45    }
46}