Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass arguments through to cargo test #530

Merged
merged 6 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/commands/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions docs/src/commands/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# wasm-pack test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!!!


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
```

`cargo test -h` for a list of all options that you can pass through.
5 changes: 4 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
} else {
Ok(mv.to_string())
}
},
}
None => bail!("We can't figure out what your Rust version is- which means you might not have Rust installed. Please install Rust version 1.30.0 or higher."),
}
}
Expand Down Expand Up @@ -95,6 +95,9 @@ pub fn cargo_build_wasm(
}

/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`.
///
/// This generates the `Cargo.lock` file that we use in order to know which version of
/// wasm-bindgen-cli to use when running tests.
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");
Expand Down
2 changes: 1 addition & 1 deletion src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 11 additions & 3 deletions src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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>;
Expand All @@ -115,6 +120,7 @@ impl Test {
geckodriver,
safari,
safaridriver,
extra_options,
} = test_opts;

let crate_path = set_crate_path(path)?;
Expand Down Expand Up @@ -147,6 +153,7 @@ impl Test {
headless,
release,
test_runner_path: None,
extra_options,
})
}

Expand Down Expand Up @@ -306,6 +313,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(())
Expand Down Expand Up @@ -349,7 +357,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(())
}

Expand Down Expand Up @@ -391,7 +399,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(())
}

Expand Down Expand Up @@ -430,7 +438,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(())
}
}
8 changes: 7 additions & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &[String],
) -> Result<(), failure::Error>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
Expand All @@ -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.
Expand Down