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) -> Option<String>;
19    #[wasm_bindgen(method, js_class = "Error", js_name = toString, structural, catch)]
20    fn to_string(this: &NodeError) -> Result<String, JsValue>;
21    #[wasm_bindgen(js_name = __wbgtest_og_console_log)]
22    fn og_console_log(s: &str);
23}
24
25impl Node {
26    /// Attempts to create a new formatter for node.js
27    pub fn new() -> Node {
28        Node {}
29    }
30}
31
32impl super::Formatter for Node {
33    fn writeln(&self, line: &str) {
34        og_console_log(line);
35    }
36
37    fn stringify_error(&self, err: &JsValue) -> String {
38        // TODO: should do a checked cast to `NodeError`
39        let err = NodeError::from(err.clone());
40        err.stack().unwrap_or(err.to_string().unwrap_or("".into()))
41    }
42}
43
44/// Path to use for coverage data.
45#[wasm_bindgen]
46pub fn __wbgtest_coverage_path(
47    env: Option<String>,
48    pid: u32,
49    temp_dir: &str,
50    module_signature: u64,
51) -> String {
52    wasm_bindgen_test_shared::coverage_path(env.as_deref(), pid, temp_dir, module_signature)
53}