Skip to content

Commit 473e3db

Browse files
authored
Add environment variables to configure tests (#4295)
1 parent b3da9dc commit 473e3db

File tree

3 files changed

+67
-43
lines changed

3 files changed

+67
-43
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
* Added support for `no_std` to `link_to!`, `static_string` (via `thread_local_v2`) and `throw`.
3333
[#4277](https://github.com/rustwasm/wasm-bindgen/pull/4277)
3434

35+
* Added environment variables to configure tests: `WASM_BINDGEN_USE_BROWSER`, `WASM_BINDGEN_USE_DEDICATED_WORKER`, `WASM_BINDGEN_USE_SHARED_WORKER` `WASM_BINDGEN_USE_SERVICE_WORKER`, `WASM_BINDGEN_USE_DENO` and `WASM_BINDGEN_USE_NODE_EXPERIMENTAL`. The use of `wasm_bindgen_test_configure!` will overwrite any environment variable.
36+
[#4295](https://github.com/rustwasm/wasm-bindgen/pull/4295)
37+
3538
### Changed
3639

3740
* String enums now generate private TypeScript types but only if used.

crates/cli/src/bin/wasm-bindgen-test-runner/main.rs

+43-17
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ impl TestMode {
5454
| Self::ServiceWorker { no_modules } => no_modules,
5555
}
5656
}
57+
58+
fn env(self) -> &'static str {
59+
match self {
60+
TestMode::Node { .. } => "WASM_BINDGEN_USE_NODE_EXPERIMENTAL",
61+
TestMode::Deno => "WASM_BINDGEN_USE_DENO",
62+
TestMode::Browser { .. } => "WASM_BINDGEN_USE_BROWSER",
63+
TestMode::DedicatedWorker { .. } => "WASM_BINDGEN_USE_DEDICATED_WORKER",
64+
TestMode::SharedWorker { .. } => "WASM_BINDGEN_USE_SHARED_WORKER",
65+
TestMode::ServiceWorker { .. } => "WASM_BINDGEN_USE_SERVICE_WORKER",
66+
}
67+
}
5768
}
5869

5970
struct TmpDirDeleteGuard(PathBuf);
@@ -138,25 +149,40 @@ fn main() -> anyhow::Result<()> {
138149
// to read later on.
139150

140151
let custom_section = wasm.customs.remove_raw("__wasm_bindgen_test_unstable");
152+
let no_modules = std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok();
141153
let test_mode = match custom_section {
142-
Some(section) if section.data.contains(&0x01) => TestMode::Browser {
143-
no_modules: std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok(),
144-
},
145-
Some(section) if section.data.contains(&0x02) => TestMode::DedicatedWorker {
146-
no_modules: std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok(),
147-
},
148-
Some(section) if section.data.contains(&0x03) => TestMode::SharedWorker {
149-
no_modules: std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok(),
150-
},
151-
Some(section) if section.data.contains(&0x04) => TestMode::ServiceWorker {
152-
no_modules: std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok(),
153-
},
154-
Some(section) if section.data.contains(&0x05) => TestMode::Node {
155-
no_modules: std::env::var("WASM_BINDGEN_USE_NO_MODULE").is_ok(),
156-
},
154+
Some(section) if section.data.contains(&0x01) => TestMode::Browser { no_modules },
155+
Some(section) if section.data.contains(&0x02) => TestMode::DedicatedWorker { no_modules },
156+
Some(section) if section.data.contains(&0x03) => TestMode::SharedWorker { no_modules },
157+
Some(section) if section.data.contains(&0x04) => TestMode::ServiceWorker { no_modules },
158+
Some(section) if section.data.contains(&0x05) => TestMode::Node { no_modules },
157159
Some(_) => bail!("invalid __wasm_bingen_test_unstable value"),
158-
None if std::env::var("WASM_BINDGEN_USE_DENO").is_ok() => TestMode::Deno,
159-
None => TestMode::Node { no_modules: true },
160+
None => {
161+
let mut modes = Vec::new();
162+
let mut add_mode =
163+
|mode: TestMode| std::env::var(mode.env()).is_ok().then(|| modes.push(mode));
164+
add_mode(TestMode::Deno);
165+
add_mode(TestMode::Browser { no_modules });
166+
add_mode(TestMode::DedicatedWorker { no_modules });
167+
add_mode(TestMode::SharedWorker { no_modules });
168+
add_mode(TestMode::ServiceWorker { no_modules });
169+
add_mode(TestMode::Node { no_modules });
170+
171+
match modes.len() {
172+
0 => TestMode::Node { no_modules: true },
173+
1 => modes[0],
174+
_ => {
175+
bail!(
176+
"only one test mode must be set, found: `{}`",
177+
modes
178+
.into_iter()
179+
.map(TestMode::env)
180+
.collect::<Vec<_>>()
181+
.join("`, `")
182+
)
183+
}
184+
}
185+
}
160186
};
161187

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

guide/src/wasm-bindgen-test/browsers.md

+21-26
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
11
# Testing in Headless Browsers
22

3-
## Configure Your Test Crate
3+
## Configure via Environment Variables
44

5-
Add this to the root of your test crate, e.g. `$MY_CRATE/tests/web.rs`:
5+
By default tests run on Node.js. To target browsers you can use the `WASM_BINDGEN_USE_BROWSER` environment variable:
66

7-
```rust
8-
use wasm_bindgen_test::wasm_bindgen_test_configure;
9-
10-
wasm_bindgen_test_configure!(run_in_browser);
7+
```sh
8+
WASM_BINDGEN_USE_BROWSER=1 cargo test --target wasm32-unknown-unknown
119
```
1210

13-
Or if you need to run your tests inside a web worker, you can also
14-
configured it using the `wasm_bindgen_test_configure` macro as following
15-
snippet.
11+
The following configurations are available:
12+
- `WASM_BINDGEN_USE_DEDICATED_WORKER`: for dedicated workers
13+
- `WASM_BINDGEN_USE_SHARED_WORKER`: for shared workers
14+
- `WASM_BINDGEN_USE_SERVICE_WORKER`: for service workers
15+
- `WASM_BINDGEN_USE_DENO`: for Deno
16+
- `WASM_BINDGEN_USE_NODE_EXPERIMENTAL`: for Node.js but as an ES module
17+
18+
## Force Configuration
19+
20+
Tests can also be forced to run in a certain environment by using the
21+
`wasm_bindgen_test_configure!` macro:
1622

1723
```rust
1824
use wasm_bindgen_test::wasm_bindgen_test_configure;
1925

20-
// Run in dedicated worker.
26+
// Run in a browser.
27+
wasm_bindgen_test_configure!(run_in_browser);
28+
// Or run in a dedicated worker.
2129
wasm_bindgen_test_configure!(run_in_dedicated_worker);
22-
// Or run in shared worker.
30+
// Or run in a shared worker.
2331
wasm_bindgen_test_configure!(run_in_shared_worker);
24-
// Or run in service worker.
32+
// Or run in a service worker.
2533
wasm_bindgen_test_configure!(run_in_service_worker);
2634
// Or run in Node.js but as an ES module.
2735
wasm_bindgen_test_configure!(run_in_node_experimental);
2836
```
2937

30-
Note that although a particular test crate must target either headless browsers
31-
or Node.js, you can have test suites for both Node.js and browsers for your
32-
project by using multiple test crates. For example:
33-
34-
```
35-
$MY_CRATE/
36-
`-- tests
37-
|-- node.rs # The tests in this suite use the default Node.js.
38-
|-- node_module.rs # The tests in this suite are configured for Node.js but as an ES module.
39-
|-- dedicated_worker.rs # The tests in this suite are configured for dedicated workers.
40-
|-- shared_worker.rs # The tests in this suite are configured for shared workers.
41-
|-- service_worker.rs # The tests in this suite are configured for service workers.
42-
`-- web.rs # The tests in this suite are configured for browsers.
43-
```
38+
Note that this will ignore any environment variable set.
4439

4540
## Configuring Which Browser is Used
4641

0 commit comments

Comments
 (0)