Skip to content

Commit

Permalink
Add tests for node-module target
Browse files Browse the repository at this point in the history
  • Loading branch information
Systemcluster committed Aug 3, 2024
1 parent 458a125 commit 14894d2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
18 changes: 12 additions & 6 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod shell;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum TestMode {
Node,
Node { no_modules: bool },
Deno,
Browser { no_modules: bool },
DedicatedWorker { no_modules: bool },
Expand All @@ -45,9 +45,9 @@ impl TestMode {

fn no_modules(self) -> bool {
match self {
Self::Node => true,
Self::Deno => true,
Self::Browser { no_modules }
| Self::Node { no_modules }
| Self::DedicatedWorker { no_modules }
| Self::SharedWorker { no_modules }
| Self::ServiceWorker { no_modules } => no_modules,
Expand Down Expand Up @@ -150,16 +150,19 @@ fn main() -> anyhow::Result<()> {
Some(section) if section.data.contains(&0x04) => TestMode::ServiceWorker {
no_modules: std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok(),
},
Some(section) if section.data.contains(&0x05) => TestMode::Node {
no_modules: std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok(),
},
Some(_) => bail!("invalid __wasm_bingen_test_unstable value"),
None if std::env::var("WASM_BINDGEN_USE_DENO").is_ok() => TestMode::Deno,
None => TestMode::Node,
None => TestMode::Node { no_modules: true },
};

let headless = env::var("NO_HEADLESS").is_err();
let debug = env::var("WASM_BINDGEN_NO_DEBUG").is_err();

// Gracefully handle requests to execute only node or only web tests.
let node = test_mode == TestMode::Node;
let node = matches!(test_mode, TestMode::Node { .. });

if env::var_os("WASM_BINDGEN_TEST_ONLY_NODE").is_some() && !node {
println!(
Expand Down Expand Up @@ -200,7 +203,8 @@ fn main() -> anyhow::Result<()> {
shell.status("Executing bindgen...");
let mut b = Bindgen::new();
match test_mode {
TestMode::Node => b.nodejs(true)?,
TestMode::Node { no_modules: true } => b.nodejs(true)?,
TestMode::Node { no_modules: false } => b.nodejs_module(true)?,
TestMode::Deno => b.deno(true)?,
TestMode::Browser { .. }
| TestMode::DedicatedWorker { .. }
Expand Down Expand Up @@ -229,7 +233,9 @@ fn main() -> anyhow::Result<()> {
let args: Vec<_> = args.collect();

match test_mode {
TestMode::Node => node::execute(module, &tmpdir, &args, &tests)?,
TestMode::Node { no_modules } => {
node::execute(module, &tmpdir, &args, &tests, !no_modules)?
}
TestMode::Deno => deno::execute(module, &tmpdir, &args, &tests)?,
TestMode::Browser { .. }
| TestMode::DedicatedWorker { .. }
Expand Down
16 changes: 13 additions & 3 deletions crates/cli/src/bin/wasm-bindgen-test-runner/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ pub fn execute(
tmpdir: &Path,
args: &[OsString],
tests: &[String],
module_format: bool,
) -> Result<(), Error> {
let mut js_to_execute = format!(
r#"
const {{ exit }} = require('process');
const wasm = require("./{0}");
{exit};
{wasm};
{console_override}
Expand All @@ -67,7 +68,16 @@ pub fn execute(
const tests = [];
"#,
module,
wasm = if !module_format {
format!(r"const wasm = require('./{0}')", module)
} else {
format!(r"import * as wasm from './{0}'", module)
},
exit = if !module_format {
r"const { exit } = require('node:process')".to_string()
} else {
r"import { exit } from 'node:process'".to_string()
},
console_override = SHARED_SETUP,
);

Expand Down
3 changes: 3 additions & 0 deletions crates/test/sample/tests/node-module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_node);

pub mod common;
6 changes: 6 additions & 0 deletions crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ macro_rules! wasm_bindgen_test_configure {
pub static __WBG_TEST_RUN_IN_SERVICE_WORKER: [u8; 1] = [0x04];
$crate::wasm_bindgen_test_configure!($($others)*);
);
(run_in_node $($others:tt)*) => (
#[link_section = "__wasm_bindgen_test_unstable"]
#[cfg(target_arch = "wasm32")]
pub static __WBG_TEST_RUN_IN_NODE: [u8; 1] = [0x05];
$crate::wasm_bindgen_test_configure!($($others)*);
);
() => ()
}

Expand Down

0 comments on commit 14894d2

Please sign in to comment.