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

feat: assertion library + smoke tests for Web APIs #45

Merged
merged 3 commits into from
Feb 15, 2023
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
36 changes: 21 additions & 15 deletions runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,28 @@ impl ModuleLoader for ZinniaModuleLoader {
));
}

if !specifier
.as_str()
.eq_ignore_ascii_case(main_js_module.as_str())
{
let mut msg =
"Zinnia does not support importing from other modules yet. "
.to_string();
msg.push_str(specifier.as_str());
if let Some(referrer) = &maybe_referrer {
msg.push_str(" imported from ");
msg.push_str(referrer.as_str());
let spec_str = specifier.as_str();

let code = {
if spec_str.eq_ignore_ascii_case(main_js_module.as_str()) {
read_file_to_string(specifier.to_file_path().unwrap()).await?
} else if spec_str == "https://deno.land/std@0.177.0/testing/asserts.ts"
{
// Temporary workaround until we implement ES Modules
// https://github.com/filecoin-station/zinnia/issues/43
include_str!("./vendored/asserts.bundle.js").to_string()
} else {
let mut msg =
"Zinnia does not support importing from other modules yet. "
.to_string();
msg.push_str(specifier.as_str());
if let Some(referrer) = &maybe_referrer {
msg.push_str(" imported from ");
msg.push_str(referrer.as_str());
}
return Err(anyhow!(msg));
}
return Err(anyhow!(msg));
}

let code = read_file_to_string(specifier.to_file_path().unwrap()).await?;
};

let module = ModuleSource {
code: Box::from(code.as_bytes()),
Expand Down
41 changes: 41 additions & 0 deletions runtime/tests/js/webapis_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts";

test("AbortController", () => {
assertEquals(typeof AbortController, "function", "typeof AbortController");
assertEquals(AbortController.name, "AbortController", "AbortController.name");
});

test("atob & btoa", () => {
assertEquals(btoa("some text"), "c29tZSB0ZXh0", `btoa("some text)`);
assertEquals(atob("c29tZSB0ZXh0"), "some text", `atob("c29tZSB0ZXh0")`);
});

test("TextEncoder", () => {
const encoder = new TextEncoder();
const bytes = encoder.encode("€");
assertEquals(Array.from(bytes.values()), [226, 130, 172]);
});

test("TextDecoder", () => {
let decoder = new TextDecoder();
let bytes = new Uint8Array([226, 130, 172]);
let text = decoder.decode(bytes);
assertEquals(text, "€");
});

test("URL", () => {
const url = new URL("https://filstation.app");
assertEquals(url.host, "filstation.app");
});

// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
function test(name, fn) {
try {
return fn();
} catch (err) {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}
}
1 change: 1 addition & 0 deletions runtime/tests/runtime_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ macro_rules! js_tests(

js_tests!(globals_tests);
js_tests!(timers_tests);
js_tests!(webapis_tests);

// Run all tests in a single JS file
async fn run_js_test_file(name: &str) -> Result<(), AnyError> {
Expand Down
28 changes: 28 additions & 0 deletions runtime/vendor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Run this Deno script to fetch and store Deno modules
// This is a temporary workaround until we support module imports
// See https://github.com/filecoin-station/zinnia/issues/43
//
// Run this script using the following command:
// deno --allow-all runtime/vendor.js

import { fromFileUrl } from "https://deno.land/std@0.177.0/path/mod.ts";

await vendor(
"https://deno.land/std@0.177.0/testing/asserts.ts",
"asserts.bundle.js",
);

async function vendor(url, outfile) {
const outpath = fromFileUrl(import.meta.resolve(`./vendored/${outfile}`));
const cmd = ["deno", "bundle", url, "--", outpath];
const child = Deno.run({ cmd });
const status = await child.status();
child.close();
if (!status.success) {
const reason = status.code
? `code ${status.code}`
: `signal ${status.signal}`;

throw new Error(`Process failed with ${reason}: ${cmd}`);
}
}
Loading