wasm_js/test/
mod.rs

1//! Testing a Rust crate compiled to wasm.
2use crate::child;
3use crate::PBAR;
4use anyhow::{Context, Result};
5use std::ffi::OsStr;
6use std::path::Path;
7use std::process::Command;
8
9/// Run `cargo test` with the `nightly` toolchain and targeting
10/// `wasm32-unknown-unknown`.
11pub fn cargo_test_wasm<I, K, V>(
12    path: &Path,
13    release: bool,
14    envs: I,
15    extra_options: &[String],
16) -> Result<()>
17where
18    I: IntoIterator<Item = (K, V)>,
19    K: AsRef<OsStr>,
20    V: AsRef<OsStr>,
21{
22    let mut cmd = Command::new("cargo");
23
24    cmd.envs(envs);
25    cmd.current_dir(path).arg("test");
26
27    if PBAR.quiet() {
28        cmd.arg("--quiet");
29    }
30
31    if release {
32        cmd.arg("--release");
33    }
34
35    cmd.arg("--target").arg("wasm32-unknown-unknown");
36
37    cmd.args(extra_options);
38
39    child::run(cmd, "cargo test").context("Running Wasm tests with wasm-bindgen-test failed")?;
40
41    // NB: `child::run` took care of ensuring that test output gets printed.
42    Ok(())
43}