Pass arguments through to cargo test

master
Chinedu Francis Nwafili 6 years ago
parent bedbf3de9d
commit b72051d385
No known key found for this signature in database
GPG Key ID: 3C85D8F6538D8AD9
  1. 2
      docs/src/commands/build.md
  2. 54
      docs/src/commands/test.md
  3. 12
      src/build.rs
  4. 2
      src/command/build.rs
  5. 27
      src/command/test.rs
  6. 8
      src/test/mod.rs

@ -59,7 +59,7 @@ wasm-pack build --target nodejs
| Option | Description |
|-----------|-----------------------------------------------------------------------------------------------------------------|
| `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. |
| `nomodules` | Outputs JS that use no modules. `browser` key in `package.json`. |
| `no-modules` | Outputs JS that use no modules. `browser` key in `package.json`. |
| `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. `module` key in `package.json`. `sideEffects: false` by default. |
## Scope

@ -0,0 +1,54 @@
# wasm-pack test
The `wasm-pack test` command wraps the [wasm-bindgen-test-runner](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html)
CLI allowing you to run wasm tests in different browsers without needing to install the different
webdrivers yourself.
```
wasm-pack test --help
```
## Path
The `wasm-pack test` command can be given an optional path argument.
This path should point to a directory that contains a `Cargo.toml` file. If no
path is given, the `test` command will run in the current directory.
```
# Run tests for the current directory's crate
wasm-pack test
# Run tests for a specified crate
wasm-pack test crates/crate-in-my-workspace
```
## Profile
The `test` command accepts an optional profile argument: `--release`.
If none is supplied, then a debug test build will be used.
## Test environment
Choose where to run your tests by passing in any combination of testing environment flags.
`--headless` is useful for running browser tests in a headless browser as part of a CI process.
```
wasm-pack test --node --firefox --chrome --safari --headless
```
## Extra options
The `test` command can pass extra options straight to `cargo test` even if they are not
supported in wasm-pack.
To use them you should add standalone `--` argument at the very
end of your command, and all the arguments you want to pass to cargo should go after.
Here's an example of running only tests that contain the world "apple".
```
wasm-pack test --firefox --headless -- --manifest-path=crates/my-workspace-crate/Cargo.toml apple
```

@ -93,15 +93,3 @@ pub fn cargo_build_wasm(
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(())
}
/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests");
if !debug {
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(cmd, "cargo build").context("Compilation of your program failed")?;
Ok(())
}

@ -19,7 +19,7 @@ use std::str::FromStr;
use std::time::Instant;
use PBAR;
/// Everything required to configure and run the `wasm-pack init` command.
/// Everything required to configure and run the `wasm-pack build` command.
#[allow(missing_docs)]
pub struct Build {
pub crate_path: PathBuf,

@ -78,6 +78,10 @@ pub struct TestOptions {
#[structopt(long = "release", short = "r")]
/// Build with the release profile.
pub release: bool,
#[structopt(last = true)]
/// List of extra options to pass to `cargo test`
pub extra_options: Vec<String>,
}
/// A configured `wasm-pack test` command.
@ -96,6 +100,7 @@ pub struct Test {
headless: bool,
release: bool,
test_runner_path: Option<PathBuf>,
extra_options: Vec<String>
}
type TestStep = fn(&mut Test, &Step) -> Result<(), Error>;
@ -115,6 +120,7 @@ impl Test {
geckodriver,
safari,
safaridriver,
extra_options,
} = test_opts;
let crate_path = set_crate_path(path)?;
@ -147,6 +153,7 @@ impl Test {
headless,
release,
test_runner_path: None,
extra_options,
})
}
@ -213,7 +220,6 @@ impl Test {
step_test_safari if self.safari,
],
BuildMode::Noinstall => steps![
step_build_tests,
step_install_wasm_bindgen,
step_test_node if self.node,
step_get_chromedriver if self.chrome && self.chromedriver.is_none(),
@ -240,18 +246,6 @@ impl Test {
Ok(())
}
fn step_build_tests(&mut self, step: &Step) -> Result<(), Error> {
info!("Compiling tests to wasm...");
let msg = format!("{}Compiling tests to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg);
build::cargo_build_wasm_tests(&self.crate_path, !self.release)?;
info!("Finished compiling tests to wasm.");
Ok(())
}
fn step_install_wasm_bindgen(&mut self, step: &Step) -> Result<(), Error> {
info!("Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
@ -306,6 +300,7 @@ impl Test {
"CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER",
&self.test_runner_path.as_ref().unwrap(),
)),
&self.extra_options
)?;
info!("Finished running tests in node.");
Ok(())
@ -349,7 +344,7 @@ impl Test {
envs.push(("NO_HEADLESS", "1"));
}
test::cargo_test_wasm(&self.crate_path, self.release, envs)?;
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
Ok(())
}
@ -391,7 +386,7 @@ impl Test {
envs.push(("NO_HEADLESS", "1"));
}
test::cargo_test_wasm(&self.crate_path, self.release, envs)?;
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
Ok(())
}
@ -430,7 +425,7 @@ impl Test {
envs.push(("NO_HEADLESS", "1"));
}
test::cargo_test_wasm(&self.crate_path, self.release, envs)?;
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
Ok(())
}
}

@ -10,7 +10,12 @@ use std::process::Command;
/// Run `cargo test` with the `nightly` toolchain and targeting
/// `wasm32-unknown-unknown`.
pub fn cargo_test_wasm<I, K, V>(path: &Path, release: bool, envs: I) -> Result<(), failure::Error>
pub fn cargo_test_wasm<I, K, V>(
path: &Path,
release: bool,
envs: I,
extra_options: &Vec<String>,
) -> Result<(), failure::Error>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
@ -23,6 +28,7 @@ where
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options);
child::run(cmd, "cargo test").context("Running Wasm tests with wasm-bindgen-test failed")?;
// NB: `child::run` took care of ensuring that test output gets printed.

Loading…
Cancel
Save