Skip to content

Commit

Permalink
Fix some broken tests in js-sys (#2888)
Browse files Browse the repository at this point in the history
* Fix some broken tests in `js-sys`

The first one created a date in UTC and then assumed that would be the same in the local time zone (as returned by `Date.prototype.toDateString`), which isn't always the case.

The second was calling `WebAssembly.instantiateStreaming` with a `Promise<Uint8Array>`, when it expects a `Response` or `Promise<Response>`; my guess as to why this wasn't failing in CI is that browsers only started enforcing it recently, and the one being used by CI doesn't.

* Move the `WebAssembly.instantiateStreaming` test to `web-sys`
  • Loading branch information
Liamolucko committed May 9, 2022
1 parent 152816c commit 820c5f1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions crates/js-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ wasm-bindgen = { path = "../..", version = "0.2.80" }
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = { path = '../test', version = '=0.3.30' }
wasm-bindgen-futures = { path = '../futures', version = '0.4.30' }
web-sys = { path = "../web-sys", version = "0.3.57", features = ["Headers", "Response", "ResponseInit"] }
7 changes: 6 additions & 1 deletion crates/js-sys/tests/wasm/Date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,12 @@ fn set_utc_seconds() {

#[wasm_bindgen_test]
fn to_date_string() {
let date = Date::new(&"05 October 2011 14:48 UTC".into());
// Create the date from date components rather than a string because this
// constructor interprets it as a date in the local time zone, which is how
// `toDateString` outputs it. This makes sure that this test will work in any
// time zone.
// October is 9 rather than 10 because the months count from 0.
let date = Date::new_with_year_month_day_hr_min(2011, 9, 5, 14, 48);
assert_eq!(JsValue::from(date.to_date_string()), "Wed Oct 05 2011");
}

Expand Down
12 changes: 1 addition & 11 deletions crates/js-sys/tests/wasm/WebAssembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use js_sys::*;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;
use web_sys::{Headers, Response, ResponseInit};

#[wasm_bindgen(module = "tests/wasm/WebAssembly.js")]
extern "C" {
Expand Down Expand Up @@ -183,17 +184,6 @@ async fn instantiate_module() {
assert!(inst.is_instance_of::<WebAssembly::Instance>());
}

#[wasm_bindgen_test]
async fn instantiate_streaming() {
let response = Promise::resolve(&get_valid_wasm());
let imports = get_imports();
let p = WebAssembly::instantiate_streaming(&response, &imports);
let obj = JsFuture::from(p).await.unwrap();
assert!(Reflect::get(obj.as_ref(), &"instance".into())
.unwrap()
.is_instance_of::<WebAssembly::Instance>());
}

#[wasm_bindgen_test]
fn memory_works() {
let obj = Object::new();
Expand Down
10 changes: 10 additions & 0 deletions crates/web-sys/tests/wasm/response.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export function new_response() {
return new Response(null, {status: 501});
}

export function get_wasm_imports() {
return {
imports: {
imported_func: function () {
return 1;
}
}
};
}
27 changes: 25 additions & 2 deletions crates/web-sys/tests/wasm/response.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use js_sys::{ArrayBuffer, DataView};
use js_sys::{ArrayBuffer, DataView, Object, Promise, Reflect, WebAssembly};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;
use web_sys::Response;
use web_sys::{Headers, Response, ResponseInit};

#[wasm_bindgen(module = "/tests/wasm/response.js")]
extern "C" {
fn new_response() -> Response;
fn get_wasm_imports() -> Object;
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -44,3 +45,25 @@ async fn test_response_from_other_body() {
let output = JsFuture::from(response_b.text().unwrap()).await.unwrap();
assert_eq!(JsValue::from_str(input), output);
}

// Because it relies on `Response`, this can't go in `js-sys`, so put it here instead.
#[wasm_bindgen_test]
async fn wasm_instantiate_streaming() {
// Taken from `crates/js-sys/tests/wasm/WebAssembly.js`.
let mut wasm = *b"\x00asm\x01\x00\x00\x00\x01\x08\x02`\x01\x7f\x00`\x00\x00\x02\x19\x01\x07imports\rimported_func\x00\x00\x03\x02\x01\x01\x07\x11\x01\rexported_func\x00\x01\n\x08\x01\x06\x00A*\x10\x00\x0b";

let headers = Headers::new().unwrap();
headers.append("Content-Type", "application/wasm").unwrap();
let response = Response::new_with_opt_u8_array_and_init(
Some(&mut wasm),
ResponseInit::new().headers(&headers),
)
.unwrap();
let response = Promise::resolve(&response);
let imports = get_wasm_imports();
let p = WebAssembly::instantiate_streaming(&response, &imports);
let obj = JsFuture::from(p).await.unwrap();
assert!(Reflect::get(obj.as_ref(), &"instance".into())
.unwrap()
.is_instance_of::<WebAssembly::Instance>());
}

0 comments on commit 820c5f1

Please sign in to comment.