-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
guide: Add documentation for testing with wasm-bindgen-test
#818
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Writing Asynchronous Tests | ||
|
||
Not all tests can execute immediately and some may need to do "blocking" work | ||
like fetching resources and/or other bits and pieces. To accommodate this | ||
asynchronous tests are also supported through the `futures` and | ||
`wasm-bindgen-futures` crates. | ||
|
||
To write an asynchronous test: | ||
|
||
1. Change `#[wasm_bindgen_test]` into `#[wasm_bindgen_test(async)]` | ||
|
||
2. Change the return type of the test function to `impl Future<Item = (), Error | ||
= JsValue>` | ||
|
||
The test will pass if the future resolves without panicking or returning an | ||
error, and otherwise the test will fail. | ||
|
||
## Example | ||
|
||
```rust | ||
extern crate futures; | ||
extern crate js_sys; | ||
extern crate wasm_bindgen_futures; | ||
|
||
use futures::Future; | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_futures::JsFuture; | ||
|
||
#[wasm_bindgen_test(async)] | ||
fn my_async_test() -> impl Future<Item = (), Error = JsValue> { | ||
// Create a promise that is ready on the next tick of the micro task queue. | ||
let promise = js_sys::Promise::resolve(&JsValue::from(42)); | ||
|
||
// Convert that promise into a future and make the test wait on it. | ||
JsFuture::from(promise) | ||
.map(|x| { | ||
assert_eq!(x, 42); | ||
}) | ||
.map_err(|_| unreachable!()) | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Testing in Headless Browsers | ||
|
||
## Configure Your Test Crate | ||
|
||
Add this to the root of your test crate, e.g. `$MY_CRATE/tests/wasm.rs`: | ||
|
||
```rust | ||
use wasm_bindgen_test::wasm_bindgen_test_configure; | ||
|
||
wasm_bindgen_test_configure!(run_in_browser); | ||
``` | ||
|
||
## Configuring Which Browser is Used | ||
|
||
> ⚡ If you are using `wasm-pack`, skip this step! Instead, use `wasm-pack test | ||
> --chrome`, `wasm-pack test --firefox`, or `wasm-pack test --safari`. | ||
> `wasm-pack` will automatically download and configure the appropriate | ||
> WebDriver client for you. | ||
|
||
If one of the following environment variables is set, then the corresponding | ||
WebDriver and browser will be used. If none of these environment variables are | ||
set, then the `$PATH` is searched for a suitable WebDriver implementation. | ||
|
||
#### `GECKODRIVER=path/to/geckodriver` | ||
|
||
Use Firefox for headless browser testing, and `geckodriver` as its | ||
WebDriver. | ||
|
||
The `firefox` binary must be on your `$PATH`. | ||
|
||
[Get `geckodriver` here](https://github.com/mozilla/geckodriver/releases) | ||
|
||
#### `CHROMEDRIVER=path/to/chromedriver` | ||
|
||
Use Chrome for headless browser testing, and `chromedriver` as its | ||
WebDriver. | ||
|
||
The `chrome` binary must be on your `$PATH`. | ||
|
||
[Get `chromedriver` here](http://chromedriver.chromium.org/downloads) | ||
|
||
#### `SAFARIDRIVER=path/to/safaridriver` | ||
|
||
Use Safari for headless browser testing, and `safaridriver` as its | ||
WebDriver. | ||
|
||
This is installed by default on Mac OS. It should be able to find your Safari | ||
installation by default. | ||
|
||
## Running the Tests in the Headless Browser | ||
|
||
Once the tests are configured to run in a headless browser, executing the tests | ||
is the same: | ||
|
||
```bash | ||
cargo test --target wasm32-unknown-unknown | ||
|
||
# or, if you're using wasm-pack | ||
wasm-pack test --headless --chrome --firefox --safari | ||
``` | ||
|
||
### Debugging Headless Browser Tests | ||
|
||
> If you're using `wasm-pack`, omitting the `--headless` flag will disable | ||
> headless mode, and allow you to debug failing tests in your browser's | ||
> devtools. | ||
|
||
Set the `NO_HEADLESS=1` environment variable and the browser tests will not run | ||
headless. Instead, the tests will start a local server that you can visit in | ||
your Web browser of choices, and headless testing should not be used. You can | ||
then use your browser's devtools to debug. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Setting Up Continuous Integration with `wasm-bindgen-test` | ||
|
||
This page contains example configurations for running `wasm-bindgen-test`-based | ||
tests in various CI services. | ||
|
||
Is your favorite CI service missing? [Send us a pull | ||
request!](https://github.com/rustwasm/wasm-bindgen) | ||
|
||
## Travis CI | ||
|
||
```yaml | ||
language: rust | ||
rust: nightly | ||
|
||
addons: | ||
firefox: latest | ||
chrome: stable | ||
|
||
install: | ||
- rustup target add wasm32-unknown-unknown | ||
- cargo install wasm-bindgen-cli | ||
# Install node.js with nvm. | ||
- curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash | ||
- source ~/.nvm/nvm.sh | ||
- nvm install v10.5 | ||
# Install chromedriver. | ||
- curl --retry 5 -LO https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip | ||
- unzip chromedriver_linux64.zip | ||
# Install geckodriver. | ||
- curl --retry 5 -LO https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-linux64.tar.gz | ||
- tar xf geckodriver-v0.21.0-linux64.tar.gz | ||
|
||
script: | ||
# Test in Chrome. | ||
- CHROMEDRIVER=$(pwd)/chromedriver cargo test --target wasm32-unknown-unknown | ||
# Test in Firefox. | ||
- GECKODRIVER=$(pwd)/geckodriver cargo test --target wasm32-unknown-unknown | ||
``` | ||
|
||
## AppVeyor | ||
|
||
```yaml | ||
install: | ||
- ps: Install-Product node 10 | ||
- appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe | ||
- rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly | ||
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin | ||
- rustc -V | ||
- cargo -V | ||
- rustup target add wasm32-unknown-unknown | ||
- cargo install wasm-bindgen-cli | ||
|
||
build: false | ||
|
||
test_script: | ||
# Test in Chrome. chromedriver is installed by default in appveyor. | ||
- set CHROMEDRIVER=C:\Tools\WebDriver\chromedriver.exe | ||
- cargo test --target wasm32-unknown-unknown | ||
- set CHROMEDRIVER= | ||
# Test in Firefox. geckodriver is also installed by default. | ||
- set GECKODRIVER=C:\Tools\WebDriver\geckodriver.exe | ||
- cargo test --target wasm32-unknown-unknown | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Testing on `wasm32-unknown-unknown` with `wasm-bindgen-test` | ||
|
||
The `wasm-bindgen-test` crate is an experimental test harness for Rust programs | ||
compiled to wasm using `wasm-bindgen` and the `wasm32-unknown-unknown` | ||
target. | ||
|
||
## Goals | ||
|
||
* Write tests for wasm as similar as possible to how you normally would write | ||
`#[test]`-style unit tests for native targets. | ||
|
||
* Run the tests with the usual `cargo test` command but with an explicit wasm | ||
target: | ||
|
||
``` | ||
cargo test --target wasm32-unknown-unknown | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd imagine that a bunch of people will just copy this so ideally this would be a fast-enough starting point.
This can take minutes, so could it make sense to instead either:
option 1. Download / decompress a release binary and move it to either
~/.cargo/bin
or/usr/local/bin
option 2. Cache cargo deps (I see that the
wasm-bindgen
repo's travis seems to do this?)Happy to do either if you think it makes sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chinedufn totally -- good call! I think option 1 is more straight forward, and we could also have a comment mentioning something like
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the guidance!
For posterity #824