@@ -5,37 +5,34 @@ like fetching resources and/or other bits and pieces. To accommodate this
5
5
asynchronous tests are also supported through the ` futures ` and
6
6
` wasm-bindgen-futures ` crates.
7
7
8
- To write an asynchronous test:
9
-
10
- 1 . Change ` #[wasm_bindgen_test] ` into ` #[wasm_bindgen_test(async)] `
11
-
12
- 2 . Change the return type of the test function to `impl Future<Item = (), Error
13
- = JsValue>`
14
-
15
- The test will pass if the future resolves without panicking or returning an
16
- error, and otherwise the test will fail.
17
-
18
- ## Example
8
+ Writing an asynchronous test is pretty simple, just use an ` async ` function!
9
+ You'll also likely want to use the ` wasm-bindgen-futures ` crate to convert JS
10
+ promises to Rust futures.
19
11
20
12
``` rust
21
- extern crate futures;
22
- extern crate js_sys;
23
- extern crate wasm_bindgen_futures;
24
-
25
- use futures :: Future ;
26
13
use wasm_bindgen :: prelude :: * ;
27
14
use wasm_bindgen_futures :: JsFuture ;
28
15
29
- #[wasm_bindgen_test( async ) ]
30
- fn my_async_test () -> impl Future < Item = (), Error = JsValue > {
16
+ #[wasm_bindgen_test]
17
+ async fn my_async_test () {
31
18
// Create a promise that is ready on the next tick of the micro task queue.
32
19
let promise = js_sys :: Promise :: resolve (& JsValue :: from (42 ));
33
20
34
21
// Convert that promise into a future and make the test wait on it.
35
- JsFuture :: from (promise )
36
- . map (| x | {
37
- assert_eq! (x , 42 );
38
- })
39
- . map_err (| _ | unreachable! ())
22
+ let x = JsFuture :: from (promise ). await . unwrap ();:
23
+ assert_eq! (x , 42 );
40
24
}
41
25
```
26
+
27
+ ## Rust compiler compatibility
28
+
29
+ Note that ` async ` functions are only supported in stable from Rust 1.39.0 and
30
+ beyond. As of the time of this writing (2019-09-05) this is the Nightly channel
31
+ of Rust.
32
+
33
+ If you're using the ` futures ` crate from crates.io in its 0.1 version then
34
+ you'll want to use the ` 0.3.* ` version of ` wasm-bindgen-futures ` and the ` 0.2.8 `
35
+ version of ` wasm-bindgen-test ` . In those modes you'll also need to use
36
+ ` #[wasm_bindgen_test(async)] ` instead of using an ` async ` function. In general
37
+ we'd recommend using the nightly version with ` async ` since the user experience
38
+ is much improved!
0 commit comments