tests/web.rs - Hello wasm-pack! (original) (raw)

  1. 1. Introduction
  2. 2. Quickstart
  3. 3. Prerequisites
    1. 3.1. npm (optional)
    2. 3.2. considerations
    3. 3.3. Non-rustup setups
  4. 4. Commands
    1. 4.1. new
    2. 4.2. build
    3. 4.3. test
    4. 4.4. pack and publish
    5. 4.5. init (DEPRECATED)
  5. 5. Tutorials
    1. 5.1. Hybrid applications with Webpack
      1. 5.1.1. Getting started
        1. 5.1.2. Using your library
    2. 5.2. npm browser packages
      1. 5.2.1. Getting started
          1. 5.2.1.1. Manual Setup
        1. 5.2.2. Template deep dive
          1. 5.2.2.1. Cargo.toml
            1. 5.2.2.2. src/lib.rs
            2. 5.2.2.3. src/utils.rs
            3. 5.2.2.4. wee_alloc
            4. 5.2.2.5. tests/web.rs
        2. 5.2.3. Building your project
        3. 5.2.4. Testing your project
        4. 5.2.5. Packaging and publishing
        5. 5.2.6. Using your library
  6. 6. Cargo.toml Configuration
  7. 7. Contributing

Hello wasm-pack!

tests/web.rs

web.rs is an integration test defined with Cargo that is intended to be run in a headless web browser via the wasm-pack test command.

It contains three key parts:

  1. #[wasm_bindgen_test] functions
  2. Crate Configuration
  3. #![cfg] directives

1. #[wasm_bindgen_test] functions

The #[wasm_bindgen_test] is like the normal Rust #[test]attribute, except it defines a test accessible to WebAssembly and headless web browser testing.

Note: Eventually #[test] will work with WebAssembly as well! Currently though custom test frameworks are not stable.


# #![allow(unused_variables)]
#fn main() {
#[wasm_bindgen_test]
fn pass() {
    assert_eq!(1 + 1, 2);
}
#}

Here the pass function is a unit test which asserts that arithmetic works in WebAssembly like we'd expect everywhere else. If the test panics (such as theassert_eq! being false) then the test will fail, otherwise the test will succeed.

The reference documentation for #[wasm_bindgen_test] should have more information about defining these tests.

2. Crate Configuration

Other than the test in this module, we'll also see:


# #![allow(unused_variables)]
#fn main() {
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);
#}

Like we saw earlier in src/lib.rs the * import pulls in everything fromwasm_bindgen_test, notably the wasm_bindgen_test_configure macro and thewasm_bindgen_test attribute.

The wasm_bindgen_test_configure macro (denoted by ending in !) is used to indicate that the test is intended to execute in a web browser as opposed to Node.js, which is the default.

3. #![cfg] directives

The last part we'll notice about this crate is this statement at the top:


# #![allow(unused_variables)]
#![cfg(target_arch = "wasm32")]
#fn main() {
#}

This statement means that the test is only intended for the wasm32architecture, or the wasm32-unknown-unknown target. This enables cargo testto work in your project if the library is also being developed for other platforms by ensuring that these tests only execute in a web browser.